pyvenv vs venv vs python-virtualenv vs virtualenv and python 3
malikarumi@Tetouan2:~$ pip install virtualenv Collecting virtualenv Downloading virtualenv-12.0.7-py2.py3-none-any.whl (1.8MB) 100% |################################| 1.8MB 330kB/s malikarumi@Tetouan2:~$ pip freeze (a lot of stuff, but not virtualenv) malikarumi@Tetouan2:~$ virtualenv testvenv1 The program 'virtualenv' is currently not installed. You can install it by typing: sudo apt-get install python-virtualenv
这里发生了什么? 是python-virtualenv == pyvenv? 这还不是吗? 原始的virtualenv仍然可以使用python吗? 如果venv(太多的名称变化!!!)是标准库的一部分, https: //docs.python.org/3/library/venv.html,为什么我被告知要安装它?
当我尝试安装它时,我得到了:
malikarumi@Tetouan2:~$ sudo apt-get install python-virtualenv Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed: python-colorama python-distlib python-html5lib python-pip python-setuptools
在那时我中止了,因为在那之前安装了python3-pip给了我
Setting up python3-setuptools (3.3-1ubuntu1) ... Setting up python3-pip (1.5.4-1) ...
而且我不确定额外的软件包是否会覆盖它们或者进入python 2.7。
我读到python 3应该是未来的默认值。 由于2.7和3.4都有,并且有单独的命令,我如何不仅确保Python 3是我的默认值,而且我安装的任何东西都在那里并被它用来代替2.7?
顺便说一句,我试过sudo apt-get python3-virtualenv并得到:E:无法找到包python3-virtualenv
Ubuntu存储库中没有python3-virtualenv包。 在Ubuntu存储库中有一个python-virtualenv包,但该包不适用于在Python虚拟环境中安装Python 3.x包。 python-virtualenv仅适用于在Python虚拟环境中安装Python 2.x包。
在Python虚拟环境中安装Python 3.x包的方法是使用Python 3虚拟环境创建器( python3-virtualenv )创建Python虚拟环境。 python3-virtualenv位于Ubuntu 14.10及更高版本的默认Ubuntu存储库中。
在Ubuntu 14.04中的Python虚拟环境中安装Python 3.x软件包
sudo apt-get install virtualenvwrapper gedit .bashrc
将以下行添加到.bashrc的末尾。
source /usr/share/virtualenvwrapper/virtualenvwrapper.sh
将更改保存到.bashrc并关闭gedit。 来源.bashrc以使更改生效。
cd source .bashrc
为python3创建一个Python虚拟环境。 您只能在此Python虚拟环境中安装python3软件包。 如果您还想安装Python 2.x软件包,那么您需要创建另一个Python虚拟环境。
mkvirtualenv py3 -p /usr/bin/python3
python3的新Python虚拟环境将在~/.virtualenvs/py3
目录中创建。 .virtualenvs
是一个隐藏文件夹。
安装包。
cd~ / .virtualenvs / py3 来源箱/激活 pip3安装包名
在16.04及更高版本的Python虚拟环境中安装Python 3.x软件包
-
安装Python 3虚拟环境创建者
sudo apt install virtualenv python3-virtualenv
-
为python3创建一个Python虚拟环境。 您只能在此Python虚拟环境中安装python3软件包。 如果您还想安装Python 2.x软件包,那么您需要创建另一个Python虚拟环境。
virtualenv -p python3 env source ./env/bin/activate
python3的新Python虚拟环境将在
env
目录中创建,该目录位于当前目录中。 -
安装Python包。
cd / path / to / env / #env是python3虚拟环境的目录 来源箱/激活 pip3 install first-package-name next-package-name last-package-name
这是一个带有最新版本pip3的新python3虚拟环境,因此在其中安装Python软件包将会很有效。
好吧,它变得复杂了。 针对Ubuntu Xenial(这是我使用的Linux Mint的底层版本)显示以下内容。 让我们解决一下:
pyvenv
它指的是Python 3.3+ stdlib包的包装脚本。 但它自Python 3.6以来就被弃用了。
它也是一个PyPi包 ,它是一个虚拟环境切换器。
VENV
这是Python 3.3+ stdlib包,其目的是改进和替换PyPi virtualenv包 (参见PEP 405 )。 但它似乎还没有(至少没有特征完整)。
python-virtualenv和virtualenv
$ apt-cache show virtualenv | grep Depends Depends: python3, python3-virtualenv
正如你所看到它在Python 3上运行python2 -m virtualenv py2
python-virtualenv
包意味着像python2 -m virtualenv py2
一样使用。 让我们看看virtualenv
如何在新的Ubuntu Xenial上工作( docker run --rm -it ubuntu:xenial
):
$ virtualenv bash: virtualenv: command not found $ apt-get update ... $ apt-get install --yes --no-install-recommends virtualenv Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: ca-certificates dh-python libexpat1 libmpdec2 libpython3-stdlib libpython3.5-minimal libpython3.5-stdlib libsqlite3-0 libssl1.0.0 mime-support openssl python-pip-whl python3 python3-minimal python3-pkg-resources python3-virtualenv python3.5 python3.5-minimal Suggested packages: libdpkg-perl python3-doc python3-tk python3-venv python3-setuptools python3.5-venv python3.5-doc binutils binfmt-support Recommended packages: file The following NEW packages will be installed: ca-certificates dh-python libexpat1 libmpdec2 libpython3-stdlib libpython3.5-minimal libpython3.5-stdlib libsqlite3-0 libssl1.0.0 mime-support openssl python-pip-whl python3 python3-minimal python3-pkg-resources python3-virtualenv python3.5 python3.5-minimal virtualenv ... $ virtualenv test The executable python2 (from --python=python2) does not exist $ virtualenv -p python3 py3 Already using interpreter /usr/bin/python3 Using base prefix '/usr' New python executable in /py3/bin/python3 Also creating executable in /py3/bin/python Installing setuptools, pkg_resources, pip, wheel...done. $ . py3/bin/activate (py3) $ python --version Python 3.5.2 (py3) $ deactivate $ apt-get install --yes --no-install-recommends python Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: libffi6 libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python-minimal python2.7 python2.7-minimal Suggested packages: python-doc python-tk python2.7-doc binutils binfmt-support The following NEW packages will be installed: libffi6 libpython-stdlib libpython2.7-minimal libpython2.7-stdlib python python-minimal python2.7 python2.7-minimal ... $ virtualenv py2 Running virtualenv with interpreter /usr/bin/python2 New python executable in /py2/bin/python2 Also creating executable in /py2/bin/python Installing setuptools, pkg_resources, pip, wheel...done. $ . py2/bin/activate (py2) $ python --version Python 2.7.12
正如您所看到的,即使virtualenv
包使用python3-virtualenv
,它的默认设置是创建Python 2环境。