简体   繁体   English

如何创建仅包含一个版本的 python/pip 的 python 虚拟环境?

[英]How can I create a python virtual environment that only includes one version of python/pip?

I had been under the impression that if I created a python environment with a specific python version, it would use just that version, but it seems I was mistaken:我的印象是,如果我使用特定的 python 版本创建 python 环境,它将只使用该版本,但似乎我错了:

$ /usr/bin/python3 -m venv pyenv
$ ls pyenv
$ ls pyenv/bin
activate      activate.fish  easy_install      pip   pip3.8  python3
activate.csh  Activate.ps1   easy_install-3.8  pip3  python

Is there any reason why older versions of python need to exist alongside the current one in my venv?为什么旧版本的 python 需要与我的 venv 中的当前版本一起存在? And can I specify not to, or should I just manually delete the python2/pip?我可以指定不这样做,还是应该手动删除 python2/pip?

In your virtual environment, all the python* executables will point to the same version.在您的虚拟环境中,所有python*可执行文件都将指向相同的版本。 You can see this from the symbolic links:您可以从符号链接中看到这一点:

$ /usr/bin/python3 -m venv pyenv

$ cd pyenv/bin

$ ls -l python*
lrwxrwxrwx 1 [user] [group]  7 May 28 05:49 python -> python3
lrwxrwxrwx 1 [user] [group] 16 May 28 05:49 python3 -> /usr/bin/python3

Similarly all the pip executables will be copies of the same file:同样,所有pip可执行文件将是同一文件的副本:

$ ls -l pip*
-rwxr-xr-x 1 [user] [group] 212 May 28 05:49 pip
-rwxr-xr-x 1 [user] [group] 212 May 28 05:49 pip3
-rwxr-xr-x 1 [user] [group] 212 May 28 05:49 pip3.6

$ diff3 pip*
[no output]

This means, for example, that once you have run the activate script so that this bin directory is in your PATH , you will find the python in your virtual environment regardless of whether you type python or python3 .这意味着,例如,一旦您运行了activate脚本,以便此bin目录位于您的PATH中,无论您键入python还是python3 ,您都会在虚拟环境中找到python

The same is true of python scripts which you invoke with #!/usr/bin/env python or #!/usr/bin/env python3 for example.例如,您使用#!/usr/bin/env python#!/usr/bin/env python3调用的 python 脚本也是如此。

Only if you explicitly invoke a different version of python (for example python2 ) or give a full path to the python executable (for example /usr/bin/python ) might you find another version located elsewhere on your system.只有当您显式调用 python 的不同版本(例如python2 )或提供 python 可执行文件的完整路径(例如/usr/bin/python )时,您可能会在系统的其他位置找到另一个版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 虚拟环境最终会使用比创建它的版本旧的 PIP 版本……为什么以及如何解决此版本控制问题? - A Python virtual environment ends up with an older version of PIP than the one that created it… why and how can I fix this versioning issue? Python:如何在临时目录创建虚拟环境并使用 pip 安装模块 - Python: How to create a virtual environment at temp dir and install modules with pip 如何创建一个没有 python 库的虚拟环境? - How can I create a virtual environment in which there will be no python libraries? 如何创建独立于OS和Python版本的Python虚拟环境 - How to create a Python virtual environment independent of OS and Python version 如何为Python指定虚拟环境 - How can I specify virtual environment for Python Python:如何在 pyenv-virtual-environment 中更新 python 版本? - Python: How can I update python version in pyenv-virtual-environment? 创建特定版本的 Python 虚拟环境 - Create Python Virtual Environment with Specific Version 如何指定用于创建虚拟环境的 python 版本? - How to specify python version used to create Virtual Environment? 尝试在python 3.5版本中创建虚拟环境 - Trying to create a virtual environment in python 3.5 version 有没有办法用早期版本的python创建虚拟环境? - Is there a way to create a virtual environment with an earlier version of python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM