无法导入在新的Conda环境中安装的软件包 [英] Cannot import packages installed in new Conda environment

查看:632
本文介绍了无法导入在新的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在该环境中看不到自己,而是在使用基本环境...因此,我无法使用在新环境中安装的软件包,但可以在基本环境中看到它们环境...

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...

这是我所做的

我按如下方式安装我的环境:

I install my environment as follows:

$ 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

这篇关于无法导入在新的Conda环境中安装的软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆