Virtualenv - Python 3 - Ubuntu 14.04 64 位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29934032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:10:52  来源:igfitidea点击:

Virtualenv - Python 3 - Ubuntu 14.04 64 bit

pythonpython-3.xpipvirtualenvubuntu-14.04

提问by timbram

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

我正在尝试在 Ubuntu 64 位 14.04 上为 Python 3 安装 virtualenv。

I have installed pip for Python3 using:

我已经使用以下方法为 Python3 安装了 pip:

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)

一切正常。现在虽然我正在尝试使用 virtualenv 命令来实际创建环境并收到未安装的错误(我猜是因为我还没有为 Python 2 安装它,这就是它试图使用的)

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

如何为 Python 3 使用 virtualenv?我已经搜索了文档,但看不到它在哪里说要做什么。

采纳答案by Celaxodon

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

我在 OS X 上的开发环境中遇到了同样的问题,我可以通过简单地调用virtualenv和目标目录的路径来创建 Python 3 虚拟环境。您应该能够通过以下两种方式之一创建 Python 3.x 虚拟环境:

  1. Install virtualenvfrom 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 venvmodule, 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

  1. virtualenv从 PyPi安装( $ pip3 install virtualenv),然后从命令行将其作为模块调用:

    $ python3 -m virtualenv /path/to/directory

  2. 使用venv模块,您可以通过apt-get. (请注意,Python 3.3 是引入此模块的时间,因此此答案假定您至少正在使用该模块):

    $ sudo apt-get install python3.4-venv

    然后你可以设置你的虚拟环境

    $ pyvenv-3.4 /path/to/directory

    并激活环境

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

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

您还可以查看这篇文章,其中讨论了venv模块和virtualenv. 祝你好运!

回答by Andrew_1510

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

venv成为从python3 V3.3标准库。因此,如果您获得更新的 python3 版本,则始终可以通过以下方式完成:

  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.

无需事先安装或下载任何东西,成功后,pip3 将附带刚刚创建的 virtualenv。通过这种方式,在大多数 Linux 上,它会打印出消息告诉您要做什么,例如它需要python3.4-venv.

To active the virtualenv

激活 virtualenv

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

回答by Kirill kilo-code Babkin

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

除了所有答案之外,您还可以使用以下命令。

virtualenv venv --python=python3.5

回答by Farhadur Reja Fahim

Also you can use this command:

你也可以使用这个命令:

virtualenv -p python3 envname

回答by Christopher Hunter

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

如果您使用的是 ubuntu 14.04.1,请澄清一下,该python3.4-venv软件包不可用(尽管它在 14.04.5 中)

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

您可以通过安装python-virtualenv软件包并通过其他答案中描述的方法之一创建 virtualenv 来解决此问题:

virtualenv -p python3 envname

or

或者

virtualenv envname --python=python3.x

回答by pyAddict

Just follow below commands:

只需遵循以下命令:

step-1 pip3 install virtualenv(if using python3)

步骤 1 pip3 install virtualenv(如果使用 python3)

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

第 2 步mkdir ~/my_environment(要创建 vir-env 的目录)

step-3 python3 -m virtualenv ~/my_environment

步骤 3 python3 -m virtualenv ~/my_environment

step-4 source ~/my_environment/bin/activate

第四步 source ~/my_environment/bin/activate

Done!!

完毕!!

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

我宁愿建议创建一个别名来在 bashrc 上激活这个 vir-env

step-1 vim ~/.bashrc

第1步 vim ~/.bashrc

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

step-2 alias myenv='source ~/my_environment/bin/activate'#在底部添加这一行

step-3 :wq#save the file using

step-3 : #保存wq文件使用

step-4 source ~/.bashrc

第四步 source ~/.bashrc

step-5 myenv#check your shortcut(alias)

step-5 myenv#检查你的快捷方式(别名)

Voyla Done!!

沃伊拉完成!!