Virtualenv - Python 3 - Ubuntu 14.04 64 bit

2020-02-17 05:31发布

问题:

I am trying to install virtualenv for Python 3 on Ubuntu 64bit 14.04.

I have installed pip for Python3 using:

pip3 install virtualenv

and everything works fine. Now though I am trying to use virtualenv command to actually create the environment and getting the error that it is not install (i guess because I haven't installed it for Python 2 and that is what it is trying to use)

How do I use the virtualenv for Python 3? I have searched the documentation but can't see where it says what to do.

回答1:

I had the same issue coming from development environments on OS X where I could create Python 3 virtual environments by simply invoking virtualenv and the path to the target directory. You should be able to create a Python 3.x virtual environment in one of two ways:

  1. Install virtualenv from the PyPi as you've done ($ pip3 install virtualenv), then by calling it as a module from the command line:

    $ python3 -m virtualenv /path/to/directory

  2. Use the venv module, which you can install through apt-get. (Note that Python 3.3 is when this module was introduced, so this answer assumes you're working with at least that):

    $ sudo apt-get install python3.4-venv

    Then you can set up your virtual environment with

    $ pyvenv-3.4 /path/to/directory

    and activate the environment with

    $ source /path/to/directory/bin/activate

You might also look at this post, which discusses differences between the venv module and virtualenv. Best of luck!



回答2:

in addition to all the answers, you can use the following command.

virtualenv venv --python=python3.5


回答3:

Also you can use this command:

virtualenv -p python3 envname


回答4:

The venv became standard library from python3 v3.3. So if you get more recent python3 version, this can always done by:

  python3 -m venv <path-or-name-of-virtualenv>
  # choose correct python3, which is the name of your python3 cmd

No need to install or download anything before hand, when succeeded, pip3 will come with the virtualenv just created. By this way, on most Linux, it will print out message to tell you what to do, for example it need python3.4-venv.

To active the virtualenv

 source <path-to-the-virtualenv>/bin/activate
 # then to deactive it:
 deactivate


回答5:

Just as a point of clarification if you are on ubuntu 14.04.1, the python3.4-venv package is not available (though it is in 14.04.5)

You can get around this by installing the python-virtualenv package and creating virtualenvs via one of the methods described in the other answers:

virtualenv -p python3 envname

or

virtualenv envname --python=python3.x


回答6:

Just follow below commands:

step-1 pip3 install virtualenv (if using python3)

step-2 mkdir ~/my_environment (dir where you want to create your vir-env)

step-3 python3 -m virtualenv ~/my_environment

step-4 source ~/my_environment/bin/activate

Done!!

I would rather suggest to create an alias for activating this vir-env on bashrc

step-1 vim ~/.bashrc

step-2 alias myenv='source ~/my_environment/bin/activate' #add this line at the bottom

step-3 :wq #save the file using

step-4 source ~/.bashrc

step-5 myenv #check your shortcut(alias)

Voyla Done!!