如何使用python3创建虚拟环境

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43069780/
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 22:28:40  来源:igfitidea点击:

How to create virtual env with python3

pythondjangopython-3.xvirtualenv

提问by Anish

I am using python 2.7+ virtualenv version 1.10.1for running myproject projects. Due to some other projects requirement I have to work with other version of python(Python 3.5) and Django 1.9. For this I have installed python in my user directory. Also I have dowloaded and installed virtualenv( version - 15.1.0) into my user directory. But whenever I am trying to create virtual env I am getting the below error

我正在使用python 2.7+ virtualenv 1.10.1 版来运行 myproject 项目。由于其他一些项目要求,我必须使用其他版本的 python( Python 3.5) 和Django 1.9。为此,我在我的用户目录中安装了 python。此外,我已将 virtualenv( version - 15.1.0) 下载并安装到我的用户目录中。但是每当我尝试创建虚拟环境时,我都会收到以下错误

python virtualenv/virtualenv.py myproject


Using base prefix '/home/myuser/python3'
New python executable in /home/mount/myuser/project_python3/myproject/bin/python
ERROR: The executable /home/mount/myuser/project_python3/myproject/bin/python is not functioning
ERROR: It thinks sys.prefix is '/home/myuser/python3' (should be '/home/mount/myuser/project_python3/myproject')
ERROR: virtualenv is not compatible with this system or executable

Can anybody tell what I am doing wrong with this

谁能告诉我我做错了什么

回答by The Aelfinn

In Python 3.6+, the pyvenv module is deprecated. Use the following one-liner instead:

在 Python 3.6+ 中,不推荐使用 pyvenv 模块。请改用以下单行:

python3 -m venv <myenvname>

This is the recommended wayto create virtual environments by the Python community.

这是Python 社区推荐的创建虚拟环境的方法。

回答by user73657

Python already ships with its builtin "virtualenv" called venvsince version 3.3. You no longer need to install or download the virtualenvscripts for Python 3.3+.

venv自 3.3 版以来,Python 已经附带了其内置的“virtualenv” 。您不再需要安装或下载virtualenvPython 3.3+的脚本。

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

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

Check that your installation provided the pyvenvcommand that should take care of creating the "virtualenv". Arguments are similar to the classic virtualenv project.

检查您的安装是否提供了pyvenv应该负责创建“virtualenv”的命令。参数类似于经典的 virtualenv 项目。

$ pyvenv --help
usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
            [--upgrade] [--without-pip]
            ENV_DIR [ENV_DIR ...]

Creates virtual Python environments in one or more target directories.

positional arguments:
  ENV_DIR               A directory to create the environment in.

optional arguments:
  -h, --help            show this help message and exit
  --system-site-packages
                        Give the virtual environment access to the system
                        site-packages dir.
  --symlinks            Try to use symlinks rather than copies, when symlinks
                        are not the default for the platform.
  --copies              Try to use copies rather than symlinks, even when
                        symlinks are the default for the platform.
  --clear               Delete the contents of the environment directory if it
                        already exists, before environment creation.
  --upgrade             Upgrade the environment directory to use this version
                        of Python, assuming Python has been upgraded in-place.
  --without-pip         Skips installing or upgrading pip in the virtual
                        environment (pip is bootstrapped by default)

Once an environment has been created, you may wish to activate it, e.g. by
sourcing an activate script in its bin directory.

回答by cryptoKTM

To create virtual env

创建虚拟环境

virtualenv -p python3 venv_name 

This will create new python executable in baseDirectory/bin/python3

这将在 baseDirectory/bin/python3 中创建新的 python 可执行文件

How to activate newely created Venv:

如何激活新创建的 Venv:

cd baseDirectory/bin/  

source activate  

Deactivate new venv

停用新的 venv

deactivate 

回答by VIJAYA SRI

To create a virtual environment in python3:

在 python3 中创建虚拟环境:

virtualenv -p /usr/bin/python3 virtualenvname

virtualenv -p /usr/bin/python3 virtualenvname

After creating the virtual environment, we need to activate it using the below command:

创建虚拟环境后,我们需要使用以下命令激活它:

source virtualenvname/bin/activate

source virtualenvname/bin/activate

to deactivate use the below command:

停用使用以下命令:

deactivate

deactivate

回答by Chaklader Asfak Arefe

I install it using the command (for Python 3.x),

我使用命令(对于 Python 3.x)安装它,

$ python3 -m venv env

回答by Henin RK

Install virtualenvwrapper on top of virtualenv to simplify things. Follow the blog to install in easy steps: virtualenvwrapper

在 virtualenv 之上安装 virtualenvwrapper 以简化事情。按照博客的简单步骤安装:virtualenvwrapper

Steps to create it:

创建它的步骤:

  1. mkvirtualenv -p /usr/bin/python3
  2. Install packages using - pip install package_name
  3. workon- activates the virtualenv, deactivate- deactivates the viirtualenv
  1. mkvirtualenv -p /usr/bin/python3
  2. 使用 - pip install package_name 安装软件包
  3. workon- 激活 virtualenv,deactivate- 停用 viirtualenv