10

I am trying to create a virtual environment for Python 3.4 on a fresh install of Ubuntu Server 14.04. I following the instructions for the venv module at:

https://docs.python.org/3/library/venv.html#module-venv

I don't have a lot of Python 3.4 or Ubuntu experience.

When I type the command:

pyvenv testDir

I get back:

pyvenv: command not found

What is causing this?

Thomas Orozco
  • 53,284
  • 11
  • 113
  • 116
timbram
  • 1,797
  • 5
  • 28
  • 49

4 Answers4

17

Ubuntu 14.04 uses Python 2 by default, and the pyenv command does not exist in Python 2 out of the box.

You can, however, use virtualenv for the same purpose. You just need to install it!

You should:

  • Install Python 3 and virtualenv apt-get install -y python3 python-virtualenv
  • Create a Python 3 virtualenv: virtualenv -p $(which python3) testDir
  • Activate the virtual environment with source testDir/bin/activate
Thomas Orozco
  • 53,284
  • 11
  • 113
  • 116
  • So, I shouldn't worry about using the newer venv module with the pyvenv command? – timbram Apr 29 '15 at 22:08
  • When I try: virtualenv -p $(python3.4) testDir it launches me into the python interpreter and I then see: Python 3.4.0 (default, Apr 11 2014, 13:05:11) [GCC 4.8.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> quit() The executable testDir (from --python=testDir) does not exist – timbram Apr 29 '15 at 22:16
  • @timbram The command has `$(which python3)`, not `$(python3)`. – Thomas Orozco Apr 29 '15 at 22:33
  • Thanks! That works! Where did you find that command? I don't see that anywhere in the virtualenv documenation. – timbram Apr 29 '15 at 23:58
  • @timbram This is a feature of your shell (not of `virtualenv`) called [Command Substitution](http://www.tldp.org/LDP/abs/html/commandsub.html). – Thomas Orozco Apr 30 '15 at 00:00
  • Oh ok, so the -p tells it to expect you to define a python interpreter to use, and then you use the command substitution to, I guess define for it which one to use. Thanks, I will have to look into it more! :) – timbram Apr 30 '15 at 00:04
  • This is what I was looking for, thank you! It works without step 1. – dasdachs Aug 24 '15 at 14:04
  • I had to use `virtualenv --always-copy` to avoid a weird symlink issue, but otherwise, this worked. – Søren Løvborg Feb 19 '16 at 13:56
4

It's also possible to create virtualenv by python itself. python3 -m venv myenv

see documentation https://docs.python.org/3/library/venv.html

pma_
  • 810
  • 1
  • 8
  • 9
1

It's in the package python3.4-venv (Linux Mint) or python3-venv (Ubuntu - I guess).

The advantages of venv over virtualenv are that (1) it's in vanilla Python3, (2) interpreter does retain tab-completion.

Wojciech Kaczmarek
  • 2,173
  • 4
  • 23
  • 40
0

Edit the .bashrc file present in your home directory by adding below code and save the file:

# Load pyenv automatically by adding
# the following to ~/.bashrc:
export PATH="/home/'Enter systemname here'/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

After this you can now run the following command:

    exec $SHELL

Now pyenv works properly

Vamsi Shankar
  • 101
  • 1
  • 2
  • 10