Creating "virtualenv" for an existing project

Question:

I have a python on which I’ve been working on. 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?

Asked By: Gaji

||

Answers:

You can just create an virtual enviroment with virtualenv venv and start it with venv/bin/activate.
You will need to reinstall all dependencies using pip, but the rest should just work fine.

Answered By: BigZ

The key thing is creating requirements.txt.

Create a virtualenv as normal. Do not activate it yet.

Now you need to install the required packages. If you do not readily remember it, ask pip:

pip freeze > requirements.txt

Now edit requirements.txt so that only the packages you know you installed are included. 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).

Install the dependencies you’ve collected:

pip install -r requirements.txt

The dependencies will be installed into your virtualenv.

The same way you’ll be able to re-create the same env on your deployment target.

Answered By: 9000

If you are using from windows then follow the following procedure:

Step 1: Go to your root directory of existing python project

Step 2: Create virtual environment with virtualenv venv

Step 4: Go to /Scripts and type this command activate

then if you would like to install all required library , pip3 install -r requirements.txt

Answered By: Ishwor Khanal

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. Save at-least 2-3 hours of the day to recover the requirements.txt for an already existing python-project.

  1. The best way to see it through and only if you are lucky, remember from that python-project, the most import package. By most-important, I mean the package that has highest-dependency.
  2. Once, you have located this highest-dependency package, install it via pip. This will install all the dependency of the highest dependency 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. But I knew, the highest dependency was this deep-learning package called Sentence-Transformers and I installed it and with minor conflicts, resolved everything.

Best-of-luck !! Let me know if it ever helped anyone !!

Answered By: Prem Kumar Tiwari
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.