简体   繁体   English

您可以在哪里使用安装在 python 虚拟环境中的软件包?

[英]Where can you use packages installed in python virtual env?

I'm a huge beginner so I'm not very informed about how packages really work.我是一个巨大的初学者,所以我不太了解包的真正工作原理。 I know that you should create a virtualenv in your project folder to avoid version conflicts etc, and you're not supposed to put your actual project files in the virtual env.我知道您应该在项目文件夹中创建一个 virtualenv 以避免版本冲突等,并且您不应该将实际项目文件放在虚拟 env 中。 So if your project files are in your project directory on the same level as the virtualenv, can your project files "access" the things installed in the virtualenv?因此,如果您的项目文件与 virtualenv 位于同一级别的项目目录中,那么您的项目文件可以“访问”安装在 virtualenv 中的东西吗? Can files outside of your directory access packages in your virtual env?目录之外的文件可以访问虚拟环境中的包吗?

Yes, it all depends on the context.是的,这完全取决于上下文。 Your virtualenv can exist anywhere, be it in your project directory, or somewhere else.您的 virtualenv 可以存在于任何地方,无论是在您的项目目录中,还是在其他地方。

When you want to use the virtualenv, you just have to call source command on it.当你想使用 virtualenv 时,你只需要调用 source 命令就可以了。 Then whatever python command you execute on whichever file, will have access to the virtualenv.然后,无论您对哪个文件执行 python 命令,都可以访问 virtualenv。 For example, if you store your virtualenv in /home/user/project/virtualenv , then you would do例如,如果您将 virtualenv 存储在/home/user/project/virtualenv中,那么您会这样做

source /home/user/project/virtualenv/bin/activate

Then whatever you with the python, it would be the version installed in virtualenv.那么无论你使用 python,它都是安装在 virtualenv 中的版本。 You can double check if you're using the global python or the virtualenv python by doing which python .您可以通过执行which python It will either point to the global python path which is usually under /usr/bin/python or /home/user/project/virtualenv/bin/python .它将指向通常位于/usr/bin/python/home/user/project/virtualenv/bin/python下的全局 python 路径。

So normally, you first do the source command , then you can do pip install on whatever packages you need already.所以通常,您首先执行source command ,然后您可以在您需要的任何软件包上执行 pip 安装。 It will be installed in the virtualenv and it will not conflict with other projects.它将安装在 virtualenv 中,并且不会与其他项目冲突。

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

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