conda虚拟环境 pip安装相关包后无法使用 解决

conda create -n test python=3.9

问题

使用conda创建虚拟环境test后,虚拟环境中python为3.9,使用pip安装numpy等,在使用时找不到

[root@z /]# python
Python 3.9.12 | packaged by conda-forge | (main, Mar 24 2022, 23:22:55) 
[GCC 10.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
  File "", line 1, in 
ModuleNotFoundError: No module named 'numpy'

再次安装时出现,发现已经安装,但是指向到了外部的python 3.6

[root@z /]# pip install numpy
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: numpy in /data/anaconda3/lib/python3.6/site-packages (1.19.5)
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

查看文件夹目录,发现全部安装到了默认的base环境中

/anaconda3/lib/python3.6/site-packages


drwxr-xr-x  4 root root    4096 May  8  2021 alabaster
drwxr-xr-x  2 root root    4096 May  8  2021 alabaster-0.7.9-py3.6.egg-info
drwxr-xr-x  2 root root    4096 May  8  2021 anaconda_client-1.6.0-py3.6.egg-info
drwxr-xr-x 10 root root    4096 May  8  2021 anaconda_navigator
drwxr-xr-x  2 root root    4096 May  8  2021 anaconda_navigator-1.4.3-py3.6.egg-info
drwxr-xr-x  5 root root    4096 May  8  2021 astroid
drwxr-xr-x  2 root root    4096 May  8  2021 astroid-1.4.9-py3.6.egg-info
drwxr-xr-x 23 root root    4096 May  8  2021 astropy
drwxr-xr-x  2 root root    4096 May  8  2021 astropy-1.3-py3.6.egg-info
drwxr-xr-x  6 root root    4096 May  8  2021 babel
drwxr-xr-x  2 root root    4096 May  8  2021 Babel-2.3.4-py3.6.egg-info
drwxr-xr-x  4 root root    4096 May  8  2021 backports

个人认为是环境变量的问题,但是不知道虚拟环境内如何单独配置环境变量

也怀疑是anaconda的问题,原因没有找到

尝试了改变路径的方法,但是没有解决(下面的方法)

conda create --prefix=/home/test_env python=3.9

解决

python -m ensurepip
python -m pip install numpy

首先运行第一行,然后使用python安装需要的包

因为强制指定python,所以用的是python3.9的pip

安装后 pip list|grep numpy,如果在base下没有numpy,会显示没有numpy

因为使用的是python3.9的pip安装

pip list|grep numpy中的pip是外部python3.6的pip

不想重装anaconda,目前只想到这个解决办法

reference

https://blog.csdn.net/weixin_41712499/article/details/105430471

https://cxybb.com/article/weixin_41712499/105430471

https://blog.csdn.net/qq_42067550/article/details/107135755

https://www.cnblogs.com/youyouzaLearn/p/8277444.html

https://blog.csdn.net/qq_36916091/article/details/106521941

你可能感兴趣的:(conda,python,pip)