Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move installed packages to a newly created virtual environment ?

I've downloaded a lots of packages into global environment (lets say so). Now, I want to create a new virtual environment and move some of the packages to that environment. How would I do that ?

like image 310
Akchurin Alnur Avatar asked Dec 22 '18 16:12

Akchurin Alnur


2 Answers

While you could copy files/directories from the site-packages directory of your global installation into the site-packages of your virtual env, you may experience problems (missing files, binary mismatch, or others). Don't do this if you're new to python packaging mechanisms.

I would advise that you run pip freeze from your global installation to get a list of what you installed, and then store that output as a requirements.txt with your source, and put it under source management. Then run pip install -r requirements.txt after activating your virtualenv, and you'll replicate the dependencies (with the same versions) into your virtualenv.

like image 117
Nino Walker Avatar answered Sep 28 '22 08:09

Nino Walker


If you try to copy or rename a virtual environment, you will discover that the copied environment does not work. This is because a virtual environment is closely tied to both the Python it was created with, and the location it was created in. (The “relocatable” option does not work.

However, this is very easy to fix. Instead of moving/copying, just create a new environment in the new location. To create VirtualEnvironment. This way work for me or you can see the link below:

pip install virtualenv
virtualenv NameOfYourVirtualEnvironment
virtualenv NameOfYourVirtualEnvironment/bin/activate

Then, run pip freeze > requirements.txt in the old environment to create a list of packages installed in it which is in your case the global environment. With that, you can just run pip install -r requirements.txt in the new environment to install packages from the saved list. Of course, you can copy requirements.txt between machines. In many cases, it will just work; sometimes, you might need a few modifications to requirements.txt to remove OS-specific stuff.

Source:https://chriswarrick.com/blog/2018/09/04/python-virtual-environments/

And also this may it work for you: How to import a globally installed package to virtualenv folder https://gist.github.com/k4ml/4080461

like image 37
I_Al-thamary Avatar answered Sep 28 '22 08:09

I_Al-thamary