简体   繁体   中英

cannot load Python 3.5 interpreter for virtualenv

I have installed Python 3.5 through Anaconda on the OSX system. After installing and activating the virtual environment,

virtualenv venv
source venv/bin/activate

The Python version is Python 2.7.10. And while we are allowed to load the interpreter of our choice in virtualenv , "/usr/bin/" only has folders for Python 2.6 and 2.7. After finding out the Anaconda python 3.5 path ( /Users/Username/anaconda/lib/python3.5) and trying to load it,

for: virtualenv -p /Users/Username/anaconda/lib/python3.5 venv

the code returns a [Errno 13] Permission Denied

> Running virtualenv with interpreter /Users/Username/anaconda/lib/python3.5
> Traceback (most recent call last):   File "/usr/local/bin/virtualenv",
> line 11, in <module>
>     sys.exit(main())   File "/Library/Python/2.7/site-packages/virtualenv.py", line 790, in main
>     popen = subprocess.Popen([interpreter, file] + sys.argv[1:], env=env)   File
> "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py",
> line 710, in __init__
>     errread, errwrite)   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py",
> line 1335, in _execute_child
>     raise child_exception
OSError: [Errno 13] Permission denied

for: virtualenv -p /Users/Username/anaconda/bin/python3.5 venv

there seems to be another type of error...

Running virtualenv with interpreter /Users/Username/anaconda/bin/python3.5
Using base prefix '/Users/Username/anaconda'
New python executable in venv/bin/python3.5
Not overwriting existing python script venv/bin/python (you must use venv/bin/python3.5)
ERROR: The executable venv/bin/python3.5 is not functioning
ERROR: It thinks sys.prefix is '/Users/Username/.../targetfolder' (should be '/Users/Username/.../targetfolder/venv')
ERROR: virtualenv is not compatible with this system or executable
ERROR: The executable venv/bin/python3.5 is not functioning
ERROR: It thinks sys.prefix is '/Users/Username/.../targetfolder' (should be '/Users/Username/.../targetfolder/venv')
ERROR: virtualenv is not compatible with this system or executable

This error results from trying to combine incompatible versions of Python and the virtualenv tool. I'm not sure precisely where the incompatibility comes from, but I do know how to work around it.

Assuming your Python is reasonably functional and recent (read: 3.3 or later), this should always work:

/path/to/python3.5 -m venv venv

The first venv is the venv module . The second is the name of the directory where you want to create a virtualenv. This command asks Python to create a virtualenv itself, rather than shelling out to a third-party tool. Thus, we can be reasonably confident Python will do it correctly, and in particular that it will not be incompatible with itself.


Unfortunately, the version of Python installed with Anaconda cannot be described as "reasonably functional" because it lacks ensurepip . That makes it impossible for the venv module to bootstrap pip into your virtualenv. So you will need to build your venv without pip, and then install it manually:

/path/to/python3.5 -m venv --without-pip venv

Then download and run get-pip.py from within the virtualenv.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM