For one of the projects, I have created anaconda virtual environment.
I install pyyaml, and it appears it is correctly installed:
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**
But when I import yaml into python, it somehow points to the python=3.6/site-packages
, and I do not know why. It gives syntax error, which is obviously due to the incompatibility of python versions:
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):
Look at the starred line, I am using environment where python is set to 2.7, but package is being imported from site-packages of version 3.6.
How do I fix this?
Edit_1:
It is only for this package that path resolution is wrong, for other packages, it points to 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: Speculation is that somehow it prefers python=3.6/site-packages
over the other. So I tried this:
>>> 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']
As you can see, python3.6/site-packages
appears before python2.7/site-packages
, so if package is found there, then it will be used. I am not sure this is how it should happen, but as a quick hack, is there a way to edit sys.path
beforehand so that python2.7/site-packages
will be searched for?