简体   繁体   中英

Virtualenv installing modules with multiple Python versions

I am trying to start a Python 3.6 project by creating a virtualenv to keep the dependencies. I currently have both Python 2.7 and 3.6 installed on my machine, as I have been coding in 2.7 up until now and I wish to try out 3.6. I am running into a problem with the different versions of Python not detecting modules I am installing inside the virtualenv.

For example, I create a virtualenv with the command: virtualenv venv

I then activate the virtualenv and install Django with the command: pip install django

My problems arise when I activate either Python 2.7 or 3.6 with the commands py -2 or py -3 , neither of the interactive shells detect Django as being installed.

Django is only detected when I run the python command, which defaults to 2.7 when I want to use 3.6. Does anyone know a possible fix for this so I can get my virtualenv working correctly? Thanks! If it matters at all I am on a machine running Windows 7.

Create virtual environment based on python3.6

virtualenv -p python3.6 env36  

Activate it:

source env36/bin/activate

Then the venv36 has been activated, venv36 's pip is available now , you can install Django as usual, and the package would be stored under env36/lib/python3.6/site-packages :

pip install django

You have to select the interpreter when you create the virtualenv.

virtualenv --python=PYTHON36_EXE my_venv

Substitute the path to your Python 3.6 installation in place of PYTHON36_EXE . Then after you've activated, python executable will be bound to 3.6 and you can just pip install Django as usual.

The key is that pip installs things for a specific version of Python, and to a very specific location. Basically, the pip command in your virtual environment is set up specifically for the interpreter that your virtual environment is using. So even if you explicitly call another interpreter with that environment activated, it will not pick up the packages pip installed for the default interpreter.

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