简体   繁体   English

为现有项目创建“virtualenv”

[英]Creating "virtualenv" for an existing project

I have a python on which I've been working on.我有一个我一直在研究的 python。 Now I've realized that I need a virtual environment for it.现在我意识到我需要一个虚拟环境。 How can I create it for an existing project ?如何为现有项目创建它? If I do this:如果我这样做:

 virtualenv venv

will it work fine?它会正常工作吗? Or do I have to re-create my project, create virtualenv and then copy the existing files to it?还是我必须重新创建我的项目,创建 virtualenv 然后将现有文件复制到它?

You can just create an virtual enviroment with virtualenv venv and start it with venv/bin/activate .您可以使用virtualenv venv创建一个虚拟环境,并使用venv/bin/activate启动它。
You will need to reinstall all dependencies using pip, but the rest should just work fine.您将需要使用 pip 重新安装所有依赖项,但其余的应该可以正常工作。

The key thing is creating requirements.txt .关键是创建requirements.txt

Create a virtualenv as normal.像往常一样创建一个 virtualenv。 Do not activate it yet.不要激活它。

Now you need to install the required packages.现在您需要安装所需的软件包。 If you do not readily remember it, ask pip :如果您不记得它,请询问pip

pip freeze > requirements.txt

Now edit requirements.txt so that only the packages you know you installed are included.现在编辑requirements.txt以便只包含您知道已安装的软件包。 Note that the list will include all dependencies for all installed packages.请注意,该列表将包含所有已安装软件包的所有依赖项。 Remove them, unless you want to explicitly pin their versions, and know what you're doing.删除它们,除非您想明确地固定它们的版本,并且知道您在做什么。

Now activate the virtualenv (the normal source path/to/virtualenv/bin/activate ).现在激活 virtualenv(正常的source path/to/virtualenv/bin/activate )。

Install the dependencies you've collected:安装您收集的依赖项:

pip install -r requirements.txt

The dependencies will be installed into your virtualenv.依赖项将安装到您的 virtualenv 中。

The same way you'll be able to re-create the same env on your deployment target.以同样的方式,您将能够在部署目标上重新创建相同的 env。

If you are using from windows then follow the following procedure:如果您从 Windows 使用,请按照以下步骤操作:

Step 1: Go to your root directory of existing python project第 1 步:转到现有 python 项目的根目录

Step 2: Create virtual environment with virtualenv venv第 2 步:使用virtualenv venv创建虚拟环境

Step 4: Go to /Scripts and type this command activate第 4 步:转到 /Scripts 并键入此命令activate

then if you would like to install all required library , pip3 install -r requirements.txt那么如果你想安装所有需要的库, pip3 install -r requirements.txt

There is something that I would like to add to this question.我想在这个问题上补充一点。 Because, newbees always have a problem and even once in a while, I do some mistake like that.因为,新手总是有问题,甚至偶尔,我也会犯这样的错误。

If you do not have requirements.txt for an already existing python-project , then you are doomed.如果您没有已经存在的python-projectrequirements.txt ,那么您注定要失败。 Save at-least 2-3 hours of the day to recover the requirements.txt for an already existing python-project .每天至少节省 2-3 小时来恢复已经存在的python-projectrequirements.txt

  1. The best way to see it through and only if you are lucky, remember from that python-project , the most import package.看透它的最好方法,只有当你幸运的时候,记住从那个python-project ,最重要的 package。 By most-important , I mean the package that has highest-dependency .最重要的是,我的意思是具有最高依赖性的 package 。
  2. Once, you have located this highest-dependency package, install it via pip .一旦找到了这个依赖最高的package,通过pip安装它。 This will install all the dependency of the highest dependency package.这将安装最高依赖package 的所有依赖。

Now, see if it works.现在,看看它是否有效。 If it does, Voila.!如果是这样,瞧。! If it doesn't you will have get where the conflicts are and start to resolve them one-by-one.如果没有,您将了解冲突所在并开始一一解决。

I was working on such a situation recently, where there was no requirements.txt.我最近正在处理这种情况,没有 requirements.txt。 But I knew, the highest dependency was this deep-learning package called Sentence-Transformers and I installed it and with minor conflicts, resolved everything.但我知道,最大的依赖是这个名为Sentence-Transformers的深度学习 package,我安装了它,并通过轻微的冲突解决了所有问题。

Best-of-luck !!祝你好运!! Let me know if it ever helped anyone !!让我知道它是否对任何人有帮助!

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

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