使用虚拟环境时安装python模块 [英] Installation of python modules when using a virtual environment

查看:75
本文介绍了使用虚拟环境时安装python模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个月前,我开始使用 Python 进行开发.我正在使用虚拟环境,因为在我遵循的安装带有 Python 绑定的 OpenCV 的教程中强烈建议这样做.我想知道安装新模块的最佳实践是什么(在 Ubuntu 上).虚拟环境的名称是cv.从命令行安装时,是否应该在虚拟环境中?IE.我应该输入

A couple of months ago I started developing in Python. I'm using a virtual environment because this was strongly advised in a tutorial I followed to install OpenCV with Python bindings. I'm wondering what's the best practice for installing new modules (on Ubuntu). The name of the virtual environment is cv. When installing from the command line, should I be in the virtual environment or not? I.e. should I enter

pip3 install modulename

workon cv
pip3 install modulename

还是两者兼而有之?它应该有所作为吗?

or both? Should it make a difference?

推荐答案

virtualenv 有什么作用?

virtualenv 将本地 python 解释器复制到文件夹中并且,一旦激活,就会将其位置添加到您的 PATH 中 - 这意味着坐在那里的 python 可执行文件将用于运行 python 代码.本质上就是这样.

What does virtualenv do?

virtualenv copies a local python interpreter into a folder and, once activated, prepends its location to your PATH - meaning that the python executable sitting there will be used to run python code. In essence, that's it.

使用例如 virtualenv venv 创建 virtualenv 后,您可以使用 source ./venv/bin/activate - done 激活它.

After creating a virtualenv with, for example, virtualenv venv, you can activate it with with source ./venv/bin/activate - done.

如果您不确定某个 venv 是否处于活动状态,通常查看您的命令行就足够了,其中将包含其名称,如下所示:(venv) user@workstation:~$ .或者,您可以运行 python -c "import sys;print(sys.executable)",然后将打印 venv 的位置而不是 /usr/bin/python,或任何系统默认值.

If you are unsure whether a venv is active, it is usually enough to look at your command line, which will contain its name like so: (venv) user@workstation:~$ . Alternatively, you can run python -c "import sys; print(sys.executable)", which will then print the venv's location instead of /usr/bin/python, or whatever the system default is.

由于很多人使用 PyCharm,请遵循这些说明 在您的 IDE 中使用 venv.它既简单又方便,因此如果您使用 PyCharm,我建议您使用它来处理您的 venv.

Since many people use PyCharm, follow these instructions to use a venv within your IDE. It is easy and convenient, so if you use PyCharm, I would advice you to handle your venvs with it.

将开发环境彼此隔离可以为您省去很多的麻烦.也许你想尝试最新的 python 开发版本而不在你宝贵的系统上释放它,也许你需要为不同的项目使用不同版本的 python 包.在源代码更改时保持执行环境静态通常是一个非常好的主意.

Isolating development environments from each other can save you a lot of headache. Maybe you want to try the newest python dev build without unleashing it on your precious system, maybe you need different versions of python packages for different projects. Keeping the executing environment static as your source code changes is just a very good idea in general.

默认情况下,安装包所需的工具 setuptoolspipwheel 已经打包到新创建的 venv 中,你可以使用 pip install package_name 安装一个包.注意不要使用 sudo,因为这会将执行用户更改为 root 并绕过 venv 激活.

By default, the tools you need to install packages, setuptools, pip, and wheel are packed into a newly created venv already, and you can just install a package with pip install package_name. Pay attention to not use sudo, as that will change the executing user to root and bypass the venv-activation.

  • virtualenv -p pyhton3.7 venv -- 我想使用一个不同于我的默认解释器的 python 解释器,例如python3.7.需要在系统上安装上述 python 解释器
  • virtualenv --system-site-packages venv -- 我想使用 venv 中使用的 python 解释器已经安装的所有包.如果您经常使用像 numpy 这样的大包,这会很有用.
  • virtualenv venv &&源 ./venv/bin/activate &&pip install -r requirements.txt -- 在从 github 克隆一个项目(并cding 到其中)后,为其设置一个独立的 Python 工作环境.
  • virtualenv -p pyhton3.7 venv -- I want to use a python interpreter that is different from my default one, e.g. python3.7. Needs an installation of said python interpreter on the system!
  • virtualenv --system-site-packages venv -- I want to use all the packages that are already installed with the python interpreter that is used in the venv. Useful if you regularly work with big packages like numpy.
  • virtualenv venv && source ./venv/bin/activate && pip install -r requirements.txt -- After cloning a project from github (and cding into it), set up a working independent python environment for it.

这篇关于使用虚拟环境时安装python模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆