How to create a virtual environment for a specific version of Python?

I am using a Macbook. It comes with python 2.7.6 installed. I manually installed python 3.4. I develop sites using django. I used only python 3, and when I work on a project, I just put it in a virtual environment.

For example, I have a project using django-1.8 and python-3.4. To create the environment, I used the following command:

python3 -m venv myvenv

After that, I installed django and other packages inside this environment.

However, I want to work on another project using python 2.7. How to create an environment for python 2.7 and install packages inside this so that my other projects remain separate and workable at the same time?

Also, is this the best way to do something? Am I going to ruin something if I go on like this?

Edit: I tried the solution from another question. I ran the following command on the terminal:

virtualenv -p /usr/bin/python2.7 <path/to/new/virtualenv/>

I get the following error:

-bash:syntax error near unexpected token 'newline'

In addition, I tried installing virtualenv using the pip command executing this command:

pip install virtualenv

It says that the pip was not found, which is strange since I used pip countless times in my virtual environments. Am I making a stupid mistake?

+4
source share
1 answer

When you use python -m venv, the virtual environment will be created by the Python interpreter that you used to invoke the command.

virtualenv , venv :

 /path/to/python3.x -m venv

venv Python. . .

:

 virtualenv -p /usr/bin/python2.7 /home/myuser/myvenvfolder
+2

Source: https://habr.com/ru/post/1622314/


All Articles