Use Python 3 in virtualenv

This article translated from: Using Python 3 in virtualenv

Using virtualenv , I run my projects with the default version of Python (2.7). Using virtualenv , I use the default version of Python (2.7) to run the project. On one project, I need to use Python 3.4. In a project, I need to use Python 3.4.

Used the I brew install python3to install IT ON My Mac. I use brew install python3to install it on your Mac. Now, how do I create a virtualenv that uses the new version? Now, how do I create a virtualenv that uses the new version ?

eg sudo virtualenv envPython3 例如sudo virtualenv envPython3

If I try: If I try:

virtualenv -p python3 test

I get: I get:

Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local/Cellar/python3/3.4.0_1/Frameworks/Python.framework/Versions/3.4'
New python executable in test/bin/python3.4
Also creating executable in test/bin/python
Failed to import the site module
Traceback (most recent call last):
  File "/Users/user/Documents/workspace/test/test/bin/../lib/python3.4/site.py", line 67, in <module>
    import os
  File "/Users/user/Documents/workspace/test/test/bin/../lib/python3.4/os.py", line 634, in <module>
    from _collections_abc import MutableMapping
ImportError: No module named '_collections_abc'
ERROR: The executable test/bin/python3.4 is not functioning
ERROR: It thinks sys.prefix is '/Users/user/Documents/workspace/test' (should be '/Users/user/Documents/workspace/test/test')
ERROR: virtualenv is not compatible with this system or executable

#1st Floor

Reference: https://stackoom.com/question/1c2Zt/ using Python in virtualenv


#2nd Floor

simply run just run

virtualenv -p python3 envname

Update after OP's edit: Update after OP edit :

The WAS A bug in there the OP apos Version of the virtualenv, AS Described here Wallpaper . There is a bug in the version in virtualenv OP, as described herein . The problem was fixed by running: The problem was fixed by running :

pip install --upgrade virtualenv

#3rd floor

A Built-3 has Python in Support for Virtual Environments - venv . Python 3 has a virtual environment venv built-in support. It might be better to use that instead . Referring to the docs :

Creation of virtual environments is done by executing the pyvenv script: Create a virtual environment by executing the pyvenv script :

pyvenv /path/to/new/virtual/environment

Update for Python 3.6 and newer: Updates for Python 3.6 and newer :

As pawciobiel correctly comments , pyvenvis deprecated as of Python 3.6 and the new way is: When pawciobiel correctly comments, it is not recommended since Python 3.6 pyvenv, the new method is:

python3 -m venv /path/to/new/virtual/environment

#4th floor

In addition to the other answers, I recommend checking what instance of virtualenv you are executing: In addition to the other answers, I recommend checking which virtualenv instance you are executing:

which virtualenv

If this turns up something in / usr / local / bin, then it is possible - even likely -. That you installed virtualenv (possibly using an instance of easy_tools or pip) without using your system's package manager (brew in OP's case) if If there is a problem in / usr / local / bin, it may even be possible to install virtualenv (possibly using easy_tools or pip instances) without using the system's package manager (brown in OP). This was my problem. This was my problem .

Years ago-when I was even more ignorant-I had installed virtualenv and it was masking my system's package-provided virtualenv. Many years ago-when I was more ignorant-I installed virtualenv, which obscures the packages provided by my system virtualenv.

After removing this old, broken virtualenv, my problems went away. After removing this old virtualenv, my problems disappeared.


#5th Floor

I'v tried pyenv and it's very handy for switching python versions (global, local in folder or in the virtualenv): I have tried pyenv , which is very convenient for switching the python version (global, folder or local in virtualenv):

brew install pyenv

then install Python version you want: Then install the required Python version:

pyenv install 3.5.0

and simply create virtualenv with path to needed interpreter version: and simply create a virtualenv and include the path of the required interpreter version:

virtualenv -p /Users/johnny/.pyenv/versions/3.5.0/bin/python3.5 myenv

That's it, check the version: That's it, check the version :

. ./myenv/bin/activate && python -V

Also plugin for pyenv are There pyenv-virtualenv But IT DID not Work for Me somehow. There pyenv the plug pyenv-virtualenv, but it me does not work.


#6th floor

Works for me

virtualenv --no-site-packages --distribute -p /usr/bin/python3 ~/.virtualenvs/py3
Published 0 original articles · praised 75 · 560,000 views +

Guess you like

Origin blog.csdn.net/w36680130/article/details/105456549