4

I am using a Macbook. It came with python 2.7.6 installed. I manually installed python 3.4. I develop websites using django. I have only used python 3 and when I work on a project, I simply put it inside a virtual environment.

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

python3 -m venv myvenv

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

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

Also, is this the best way to do things? Am I going to mess something up if I continue like this?

Edit: I tried a 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'

Also, I tried installing virtualenv using pip running this command:

pip install virtualenv

It says pip is not found which is weird as I have used pip countless times before inside my virtual environments. Am I making a stupid mistake?

darkhorse
  • 8,192
  • 21
  • 72
  • 148

1 Answers1

4

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

To create a virtualenv to a specific interpreter, just use it to run venv module using this interpreter:

 /path/to/python3.x -m venv

venv module is not available for older Python interpreters. For those you need to install a separate virtualenv package. The actual installation depends on your operating system.

The command is then like:

 virtualenv -p /usr/bin/python2.7 /home/myuser/myvenvfolder
Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435