4

I just got Python 3.5.2 and wanted to create a virtualenv.

I have done this before. Right now, I have a virtualenv on a Python2.7 project that I can still open with source bin/activate.

But try as I might, from /home, or from /path/to/virtualenv, or /path/to/virtualenv-$, with or without sudo and python prepended on the command line, I only get one response: no such file or directory.

This was with the -p flag so it would use 3.5.2, because 2.7.12 is still my default. If it is broken, why does the virtualenv I created for 2.7 still activate?

Then I tried pyvenv and venv (which I've never used before) from the 3.5.2 interpreter:

>>> pyvenv /home/malikarumi/Projects/aishah
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'pyvenv' is not defined
>>> venv /home/malikarumi/Projects/aishah
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'venv' is not defined

and from bash:

malikarumi@Tetuoan2:~$ malikarumi@Tetuoan2:/usr/local/bin$ python3.5    pyvenv ~/Projects/aishah
bash: malikarumi@Tetuoan2:/usr/local/bin$: No such file or directory
malikarumi@Tetuoan2:~$ malikarumi@Tetuoan2:/usr/local/bin$ python3.5 venv ~/Projects/aishah
bash: malikarumi@Tetuoan2:/usr/local/bin$: No such file or directory

What am I doing wrong?

Malik A. Rumi
  • 1,855
  • 4
  • 25
  • 36
  • Run it as a module: `python3.5 -m venv ~/Projects/aishah`. Note the `-m` option. –  Oct 02 '16 at 05:59
  • Alternatively, I can recommend `virtualenvwrapper` (pip installable), which makes possibly even easier: `mkvirtualenv blah --python=/usr/local/bin/python3.5` and you're all set (and `--python=/usr/local/bin/python3.5` is likely not even necessary if you installed it with Python3.5's pip). –  Oct 02 '16 at 06:01

2 Answers2

6

Using virtualenv

First of all you should verify that virtualenv is really installed for Python 3.5:

python3.5 -m pip list

If it's not then install it either with the package manager of your distribution or by running python3.5 -m pip install virtualenv.

Then you can run python3.5 -m virtualenv <newvenv> and it should create a new virtualenv using Python 3.5 for you.

Using venv that is already part of the standard library in Python 3.5

Simply running python3.5 -m venv <newvenv> should do the job.

ThoWe
  • 346
  • 3
  • 13
  • thanks to both of you. I decided to go with the venv instead of virtualenv and this worked. the -m flag is new to me, so I've given it a look. https://www.python.org/dev/peps/pep-0338/ – Malik A. Rumi Oct 02 '16 at 11:57
  • When I try that, activate and run `python --version`, I get version 2. Any idea why? – falsePockets Aug 21 '19 at 05:50
1

If you've got more than one version of python installed, and thinking of creating a virtual environment with a specific version, say you've got python3.8 and python3.9, then you can create a virtual environment like so;

python3.8 -m venv <your preferred virtual environment name>
Chukwunazaekpere
  • 906
  • 1
  • 6
  • 13