无法导入安装在新 Conda 环境中的软件包
问题描述
我正在尝试在 HPC 服务器上创建自己的 conda python 环境,但发生了一些非常奇怪的事情.
I am trying to make my own conda python environment on HPC server, and something very strange is happening.
问题
创建一个新的 conda 环境后,python 似乎没有在这个环境中看到自己,并且正在使用 base 环境......因此我无法使用安装在新环境中的包,但我可以看到 base 中的包环境...
After creating a new conda environment, it appears that python is not seeing itself in this environment, and using the base environment... Thus I cannot use packages installed in the new environment, but I can see the ones in the base environment...
这就是我所做的
我的环境安装如下:
$ conda create -n niml pip python=3.6.5
$ source activate niml
(niml) $ conda install -c conda-forge luigi
然后我检查我安装的包:
and then I check my installed packages:
(niml) $ conda list
这是我得到的,非常基本的环境:
and here is what I get, very basic environment:
# Name Version Build Channel
botocore 1.10.61 py_0 conda-forge
ca-certificates 2018.4.16 0 conda-forge
certifi 2018.4.16 py36_0 conda-forge
docutils 0.14 py36_0 conda-forge
jmespath 0.9.3 py_1 conda-forge
libedit 3.1.20170329 h6b74fdf_2
libffi 3.2.1 hd88cf55_4
libgcc-ng 7.2.0 hdf63c60_3
libstdcxx-ng 7.2.0 hdf63c60_3
lockfile 0.12.2 py_1 conda-forge
luigi 2.7.6 py36_0 conda-forge
ncurses 6.1 hf484d3e_0
openssl 1.0.2o 0 conda-forge
pip 10.0.1 py36_0
pyparsing 2.2.0 py_1 conda-forge
python 3.6.5 hc3d631a_2
python-daemon 2.1.2 py36_0
python-dateutil 2.7.3 py_0 conda-forge
readline 7.0 ha6073c6_4
setuptools 39.2.0 py36_0
six 1.11.0 py36_1 conda-forge
sqlite 3.24.0 h84994c4_0
tk 8.6.7 hc745277_3
tornado 4.5.3 py36_0 conda-forge
wheel 0.31.1 py36_0
xz 5.2.4 h14c3975_4
zlib 1.2.11 ha838bed_2
然后我尝试运行python并导入我安装的包luigi
,它没有发现错误.
then I try running python and import the package which I installed luigi
, and it does not find it getting an error.
(niml) $ python
>>> import luigi
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'luigi'
我还尝试导入位于基础环境中而不是在新环境中的 numpy
,它可以正常工作.
I also tried to import numpy
which is in the base environment and NOT in the new environment, and it works.
>>> import numpy
上面的工作,这意味着 python
认为它是在基础环境中运行的.
The above works, which means python
thinks it is running in the base environment.
Python 正在我自己的环境中运行
查看了执行的是哪个python,确实是新环境下的那个niml
:
I checked which python is being executed, and it is indeed the one in the new environment niml
:
>>> import sys
>>> sys.executable
'~/.conda/envs/niml/bin/python'
同样从命令行检查,它是环境中的 python
可执行文件:
Also checked from the command line, and it is the python
executable within the envrinment:
(niml) $ which python
~/.conda/envs/niml/bin/python
我在 HPC 上运行
我之前已经创建了数百次 anaconda 环境,但从未遇到过这个问题.唯一的区别是我使用的是 HPC 服务器,因此我必须做这样的事情:
I have created anaconda environments hundreds of times before and never had this problem. Only difference is that I am using an HPC server, and thus I had to make something like this:
module load python/anaconda3
这是我从通常的工作流程中看到的唯一可能导致此问题的不同...
this is the only difference I see from my usual workflow which might be creating this problem...
其他人以前见过这个问题并且能够解决它吗??
Anyone else has seen this problem before and able to solve it??
解决方案
我试图解决一个类似的问题,并通过 virtualenv 而不是使用 conda 环境解决了这个问题.我相信 Anaconda 和您的机器之间存在冲突,因为两者都认为他们正在控制您的新环境,而在 virtualenv 中设置新环境似乎可以解决问题.
I was trying to solve a similar issue and solved this through virtualenv rather than using a conda environment. I believe there is a conflict between Anaconda and your machine in that both think they are controlling your new environment which setting up a new environment in virtualenv seemed to fix.
如果有帮助,这里是如何使用 virtualenv 设置环境的方法.如果您还没有位置,请为您的新环境创建一个位置:
If it's helpful, here's how to set up an environment using virtualenv. Create a location for your new environment if you don't have one already:
mkdir ~/virtualenvironment
设置您的虚拟环境:
virtualenv ~/virtualenvironment/niml/ --python=python3.6.5
激活您的环境:
source bin/activate
确保您已安装所需的任何软件包:
Make sure that you've installed whatever packages you need:
pip install luigi
检查包是否在python中正确导入:
Check that the package imports properly in python:
python
import luigi
停用:
source deactivate
相关文章