1

I am making a python 3 application (flask based) and for that I created a virtualenv in my development system, installed all packages via pip and my app worked fine.

But when I moved that virtualenv to a different system (python3 installed) and ran my application with the absolute path of my virtualenv python (c:/......./myenv/Scripts/python.exe main.py) then it threw the errors that packages are not installed, I activated the virtualenv and used pip freeze and there were no packages were installed.

But under virtualenv there is 'Site-Packages' (myenv -> lib -> site-packages) , all my installed packages were persent there.

My Question is how to use the packages that are inside 'site-packages' even after moving the virtualenv to different system in Python 3.

Sonu Gadewar
  • 27
  • 1
  • 2
  • 9

3 Answers3

4

Moving a virtualenv from a computer to another, and even on the same computer from a location to another is a bad idea , and this is why :

  • Since a lot of the binaries and libs are symlinks, and linked to your old system binaries and libs, it won't work on other machines.
  • Since many of bin/ scripts in your virtualenv depends on the virtualenv path on the system , it won't work if you moved the virtualenv to another location (even on same system either .)

So the recommend way is :

  • First generate requirements.txt file :

     pip freeze > requirements.txt
    
  • Second after moving everything (except the virtualenv directory) create a new virtualenv, activate it and run :

     pip install -r requirements.txt
    

Finally in your case if you really didn't generated a requirements.txt file, and need to use the old site-packages , there is a dirty workaround which i tried once on a gnu/linux machine and somehow worked but am not 100% sure if it will work properly so if you want give it a try.

  • copy the site-packages in your-old-virtualenv/lib/python{version}/ somewhere in your new computer , Desktop for example
  • Delete the old virtualenv, and create a new virtualenv
  • Replace the site-packages in the new virtualenv in new-virtualenv/lib/python{version} with the old site-packages
  • Delete __pycache__ folder in the newly copied site-packages
  • Activate the new virtualenv and test if everything is working .

Note that you should use the same python version either 2 or 3 , don't expect a virtualenv that depends on python2 to run properly with python3

WaLid LamRaoui
  • 2,305
  • 2
  • 15
  • 33
  • 1
    I will try this ( replacing site-packages).. but how to pip install packages to virtualenv from a python script? Because when i move my application to different systems i dont want to manually activate virtualenv and then install packages.. ( because it's a final app for a client and can't do manual install) – Sonu Gadewar Dec 23 '19 at 21:44
  • read here on how to install packages from within a python script - https://pip.pypa.io/en/latest/user_guide/#id32 , also check https://stackoverflow.com/questions/12332975/installing-python-module-within-code . and please if the work-around worked properly please mark this answer accepted – WaLid LamRaoui Dec 23 '19 at 21:57
  • Does replacing site-packages worked ? i tried that many time on Gnu/Linux systems but never on Windows . – WaLid LamRaoui Dec 24 '19 at 13:19
  • 1
    sorry for the late reply.. and no replacing site packages didn't workded.. I made a solution that i dynamically creates new virtualenv and install packages on that – Sonu Gadewar Dec 28 '19 at 18:20
  • ok..Glad to hear that you managed to make a solution, i was not sure if replacing site-packages will work properly, anyway – WaLid LamRaoui Dec 28 '19 at 18:28
0

Maybe you can consider using pipenv to control the virtualenvs on different computer or environment.

Ginta
  • 151
  • 6
0

You Must not copy & paste venv, even in the same system.
If you install new package in venv-copied, then it would installed in venv-original.
Becaus settings are bound to specific directory.

sngjuk
  • 927
  • 1
  • 10
  • 24