简体   繁体   English

如何创建独立于OS和Python版本的Python虚拟环境

[英]How to create a Python virtual environment independent of OS and Python version

I am trying to create a virtual environment to run a script which requires Python 3.6.我正在尝试创建一个虚拟环境来运行需要 Python 3.6 的脚本。 I started off with Pipenv but I am unable to create the same environment on other platforms via the Pipfile.lock or requirements.txt unless the other platform(s) has Python 3.6 installed.我从 Pipenv 开始,但我无法通过 Pipfile.lock 或 requirements.txt 在其他平台上创建相同的环境,除非其他平台安装了 Python 3.6。 I have read this post but I am unsure which direction I should take to create a virtual environment which can be shared and run its own version of Python independent of operating system and version of Python installed on the other platform.我已经阅读了这篇文章,但我不确定我应该采取哪个方向来创建一个可以共享和运行自己的 Python 版本的虚拟环境,独立于操作系统和安装在其他平台上的 Python 版本。

Virtual environments are not portable, they depend on the Python installation you have.虚拟环境不可移植,它们取决于您拥有的 Python 安装。

You can't share/distribute virtual environment with others, because you can't control which version of Python others are using.您无法与他人共享/分发虚拟环境,因为您无法控制其他人正在使用哪个版本的 Python。 If you want to distribute your code along with all dependencies including the specific version of Python interpreter, you can use PyInstaller.如果您想分发您的代码以及所有依赖项,包括 Python 解释器的特定版本,您可以使用 PyInstaller。 It is far from perfect and little bit hacky.它远非完美,而且有点hacky。 Also it generates a package which is specific to operating system.它还生成一个特定于操作系统的 package。

https://pyinstaller.readthedocs.io/en/stable/operating-mode.html https://pyinstaller.readthedocs.io/en/stable/operating-mode.html

There is also a detailed step-by-step guide on how to use PyInstaller.还有关于如何使用 PyInstaller 的详细分步指南。 https://realpython.com/pyinstaller-python/ https://realpython.com/pyinstaller-python/

This is step-by-step how I use Python virtual environment and share it with co-workers.这是我如何使用 Python 虚拟环境并与同事分享的分步说明。

To check python and virtualenv presence, run following commands:要检查 python 和 virtualenv 是否存在,请运行以下命令:

which python3
python3 -m pip list | grep env
which virtualenv

Install a python virtual environment builder:安装 python 虚拟环境构建器:

python3 -m pip install virtualenv

Create a virtual environment named venv inside the project's directory: virtualenv venv在项目目录中创建一个名为venv的虚拟环境: virtualenv venv

To activate this environment use this command inside project's directory: source venv/bin/activate要激活此环境,请在项目目录中使用此命令: source venv/bin/activate

Install python modules dependencies listed in a requirements.txt:安装 requirements.txt 中列出的 python 模块依赖项:

python3 -m pip install -r requirements.txt

You should activate virtual environment when you working with python in this directory for package installation and for running commands in the project directory.当您在此目录中使用 python 进行 package 安装和在项目目录中运行命令时,您应该激活虚拟环境。 When you need to deactivate the virtual environment do it using deactivate command.当您需要停用虚拟环境时,请使用deactivate命令。

To deactivate environment simply run: deactivate要停用环境,只需运行: deactivate

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM