Anaconda选择正确的环境但是错误的sys.path?

时间:2019-05-17 10:14:19

标签: python pip anaconda

我有不同的蟒蛇环境。启动其中之一时,似乎sys.path仍设置为完全错误的目录。我不是正确地理解了环境的概念,或者这是与我的蟒蛇设置有关的错误?

我的环境:

fabianwerner ~$ conda env list
# conda environments:
#
base                  *  /Users/fabianwerner/anaconda3/anaconda3
my-rdkit-env             /Users/fabianwerner/anaconda3/anaconda3/envs/my-rdkit-env
openpose                 /Users/fabianwerner/anaconda3/anaconda3/envs/openpose

fabianwerner ~$ 

我激活环境“ openpose”:

fabianwerner ~$ conda activate openpose
(openpose) fabianwerner ~$ 

我将尝试导入“ keras”,因此让我们确保已安装它:

(openpose) fabianwerner ~$ pip list | grep -i keras
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
Keras (2.2.4)
Keras-Applications (1.0.7)
Keras-Preprocessing (1.0.9)
You are using pip version 9.0.1, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(openpose) fabianwerner ~$ 

我启动python并尝试导入keras:

(openpose) fabianwerner ~$ python
Python 3.6.5 |Anaconda, Inc.| (default, Apr 26 2018, 08:42:37) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'keras'
>>> 

让我们检查python在哪里寻找模块:

>>> import sys
>>> print(sys.path)   
['', '/Users/fabianwerner/anaconda3/anaconda3/envs/my-rdkit-env/lib/python36.zip', '/Users/fabianwerner/anaconda3/anaconda3/envs/my-rdkit-env/lib/python3.6', '/Users/fabianwerner/anaconda3/anaconda3/envs/my-rdkit-env/lib/python3.6/lib-dynload', '/Users/fabianwerner/.local/lib/python3.6/site-packages', '/Users/fabianwerner/anaconda3/anaconda3/envs/my-rdkit-env/lib/python3.6/site-packages']
>>> 

所以我激活了环境'openpose',但是它仍然在另一个(错误的)环境'my-rdkit-env'中寻找模块!

该如何解决?

编辑:

我无法看到anaconda设置的任何明显问题,因为conda info -a提供了正确的路径:

(openpose) fabianwerner ~$ conda info -a

     active environment : openpose
    active env location : /Users/fabianwerner/anaconda3/anaconda3/envs/openpose
            shell level : 2
       user config file : /Users/fabianwerner/.condarc
 populated config files : 
          conda version : 4.5.11
    conda-build version : 3.15.1
         python version : 3.7.0.final.0
       base environment : /Users/fabianwerner/anaconda3/anaconda3  (writable)
           channel URLs : https://repo.anaconda.com/pkgs/main/osx-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/free/osx-64
                          https://repo.anaconda.com/pkgs/free/noarch
                          https://repo.anaconda.com/pkgs/r/osx-64
                          https://repo.anaconda.com/pkgs/r/noarch
                          https://repo.anaconda.com/pkgs/pro/osx-64
                          https://repo.anaconda.com/pkgs/pro/noarch
          package cache : /Users/fabianwerner/anaconda3/anaconda3/pkgs
                          /Users/fabianwerner/.conda/pkgs
       envs directories : /Users/fabianwerner/anaconda3/anaconda3/envs
                          /Users/fabianwerner/.conda/envs
               platform : osx-64
             user-agent : conda/4.5.11 requests/2.19.1 CPython/3.7.0 Darwin/17.7.0 OSX/10.13.6
                UID:GID : 502:20
             netrc file : None
           offline mode : False

# conda environments:
#
base                     /Users/fabianwerner/anaconda3/anaconda3
my-rdkit-env             /Users/fabianwerner/anaconda3/anaconda3/envs/my-rdkit-env
openpose              *  /Users/fabianwerner/anaconda3/anaconda3/envs/openpose
strokinat0r              /Users/fabianwerner/anaconda3/anaconda3/envs/strokinat0r

sys.version: 3.7.0 (default, Jun 28 2018, 07:39:16) 
...
sys.prefix: /Users/fabianwerner/anaconda3/anaconda3
sys.executable: /Users/fabianwerner/anaconda3/anaconda3/bin/python
conda location: /Users/fabianwerner/anaconda3/anaconda3/lib/python3.7/site-packages/conda
conda-build: /Users/fabianwerner/anaconda3/anaconda3/bin/conda-build
conda-convert: /Users/fabianwerner/anaconda3/anaconda3/bin/conda-convert
conda-develop: /Users/fabianwerner/anaconda3/anaconda3/bin/conda-develop
conda-env: /Users/fabianwerner/anaconda3/anaconda3/bin/conda-env
conda-index: /Users/fabianwerner/anaconda3/anaconda3/bin/conda-index
conda-inspect: /Users/fabianwerner/anaconda3/anaconda3/bin/conda-inspect
conda-metapackage: /Users/fabianwerner/anaconda3/anaconda3/bin/conda-metapackage
conda-render: /Users/fabianwerner/anaconda3/anaconda3/bin/conda-render
conda-server: /Users/fabianwerner/anaconda3/anaconda3/bin/conda-server
conda-skeleton: /Users/fabianwerner/anaconda3/anaconda3/bin/conda-skeleton
user site dirs: ~/.local/lib/python3.6

CIO_TEST: <not set>
CONDA_DEFAULT_ENV: openpose
CONDA_EXE: /Users/fabianwerner/anaconda3/anaconda3/bin/conda
CONDA_PREFIX: /Users/fabianwerner/anaconda3/anaconda3/envs/openpose
CONDA_PREFIX_1: /Users/fabianwerner/anaconda3/anaconda3
CONDA_PROMPT_MODIFIER: (openpose) 
CONDA_PYTHON_EXE: /Users/fabianwerner/anaconda3/anaconda3/bin/python
CONDA_ROOT: /Users/fabianwerner/anaconda3/anaconda3
CONDA_SHLVL: 2
PATH: /Users/fabianwerner/anaconda3/anaconda3/envs/openpose/bin:/Users/fabianwerner/anaconda3/anaconda3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/GIMP-2.10.app/Contents/MacOS
REQUESTS_CA_BUNDLE: <not set>
SSL_CERT_FILE: <not set>


WARNING: could not import _license.show_info
# try:
# $ conda install -n root _license
(openpose) fabianwerner ~$ 

此外,我似乎没有设置与python相关的环境变量:

(openpose) fabianwerner ~$ printenv | grep -i python
CONDA_PYTHON_EXE=/Users/fabianwerner/anaconda3/anaconda3/bin/python
(openpose) fabianwerner ~$ 

我的设置:

  • Mac OS-X 10.13.6(High Sierra)
  • anaconda版本4.5.11

谢谢

固件

1 个答案:

答案 0 :(得分:1)

您在示例中启动的Python解释器不是环境中的那个。 conda info -apython version : 3.7.0.final.0,而您的口译员说Python 3.6.5

当您激活环境并运行which python时,问题应该变得很明显,该环境应该指向已激活的环境,但可能并非如此。

您是如何创建这些环境的?确保设置python=XX选项,否则新环境将使用基础/根环境中的解释器,而不是安装新的解释器。即conda create -n my_environment python=3.7

编辑: 抱歉,我只是抬起头来并测试了conda info -apython version : XX似乎是指基本环境,而不是当前活动的环境。

我在这里留下这个答案,因为即使我的推理似乎是错误的,它仍然可能会有所帮助。