无法使用python = 2.7导入yaml - anaconda虚拟环境

时间:2018-05-22 21:07:47

标签: python-2.7 anaconda yaml

对于其中一个项目,我创建了anaconda虚拟环境。

我安装了pyyaml,它似乎已正确安装:

conda list
# packages in environment at /home/<myname>/anaconda3/envs/Alex:
#
# Name                    Version                   Build  Channel
autobahn                  18.5.1                    <pip>
Automat                   0.6.0                     <pip>
backports.functools-lru-cache 1.5                       <pip>
ca-certificates           2018.03.07                    0  
certifi                   2018.4.16                py27_0  
constantly                15.1.0                    <pip>
Cython                    0.23.5                    <pip>
enum34                    1.1.6                     <pip>
Flask                     1.0.2                     <pip>
hyperlink                 18.0.0                    <pip>
incremental               17.5.0                    <pip>
ipdb                      0.11                      <pip>
ipython                   5.7.0                     <pip>
kiwisolver                1.0.1                     <pip>
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  
matplotlib                2.2.2                     <pip>
ncurses                   6.1                  hf484d3e_0  
numpy                     1.14.3                    <pip>
openssl                   1.0.2o               h20670df_0  
pip                       10.0.1                    <pip>
pip                       10.0.1                   py27_0  
pockets                   0.6.2                     <pip>
protobuf                  3.5.2.post1               <pip>
PyAudio                   0.2.11                    <pip>
pyga                      2.5.1                     <pip>
pysqlite                  2.8.3                     <pip>
pystache                  0.5.4                     <pip>
python                    2.7.15               h1571d57_0  
python-Levenshtein        0.12.0                    <pip>
**pyyaml                    3.12             py27h2d70dd7_1**  

但是当我将yaml导入python时,它以某种方式指向python=3.6/site-packages,我不知道为什么。它给出了语法错误,这显然是由于python版本不兼容:

Python 2.7.15 |Anaconda, Inc.| (default, May  1 2018, 23:32:55) 
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import yaml
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  **File "/home/<myname>/anaconda3/lib/python3.6/site-packages/yaml/__init__.py", line 284**
    class YAMLObject(metaclass=YAMLObjectMetaclass):

查看已加星标的行,我正在使用python设置为2.7的环境,但是从3.6版的site-packages导入包。

我该如何解决这个问题?

Edit_1: 只有这个包的路径解析是错误的,对于其他包,它指向python=2.7/site-packages

>>> import pyga
>>> pyga.__path__
['/home/<myname>/anaconda3/envs/Alex/lib/python2.7/site-packages/pyga']
>>> import pockets
>>> pockets.__path__
['/home/<myname>/anaconda3/envs/Alex/lib/python2.7/site-packages/pockets']
>>> import scipy
>>> scipy.__path__
['/home/<myname>/anaconda3/envs/Alex/lib/python2.7/site-packages/scipy']
>>>

Edit_2 :猜测是某种方式它更喜欢python=3.6/site-packages而不是另一种。所以我尝试了这个:

>>> import sys
>>> sys.path
['', '/home/<myname>/anaconda3/lib/python3.6/site-packages', 

'/home/<myname>/anaconda3/envs/Alex/lib/python27.zip', 

'/home/<myname>/anaconda3/envs/Alex/lib/python2.7', 

'/home/<myname>/anaconda3/envs/Alex/lib/python2.7/plat-linux2', 

'/home/<myname>/anaconda3/envs/Alex/lib/python2.7/lib-tk', 

'/home/<myname>/anaconda3/envs/Alex/lib/python2.7/lib-old', 

'/home/<myname>/anaconda3/envs/Alex/lib/python2.7/lib-dynload', 

'/home/<myname>/anaconda3/envs/Alex/lib/python2.7/site-packages', 

'/home/<myname>/anaconda3/envs/Alex/lib/python2.7/site-packages/pysox-0.3.6a0-py2.7-linux-x86_64.egg']

正如您所看到的,python3.6/site-packages出现在python2.7/site-packages之前,因此如果在那里找到了包,那么它将被使用。我不确定这是应该发生的方式,但作为一种快速黑客攻击,有没有办法事先编辑sys.path以便搜索python2.7/site-packages

1 个答案:

答案 0 :(得分:0)

我想我已经解决了这个问题,

输入:(推荐表格this link

unset PYTHONPATH

然后

>>> import yaml
>>> yaml.__path__
['/home/<myname>/anaconda3/envs/Alex/lib/python2.7/site-packages/yaml']
>>>