40

I started using Windows and Linux recently on the same PC - they are installed to two different partitions, and a third partition contains common data and archives. virtualenvs created from Windows are made with folder "Scripts", and the counterpart in Linux is the folder "bin".

The problem here is that the files in those folders are not compatible for both OSes. For example, the "activate" contained in bin (created in Linux) don't run in Windows, and by the other hand, the "activate" in Scripts (created in Windows) cannot be executed on Linux.

Is there a way to use the same virtualenv on both OSes?

alex
  • 6,818
  • 9
  • 52
  • 103
Cristianjs19
  • 765
  • 1
  • 9
  • 15
  • Follow this answer to learn how to produce identical virtualenvs in the two OSes (ignore the change from python 2 to 3) http://stackoverflow.com/a/41390096/267540 – e4c5 Mar 11 '17 at 09:31

1 Answers1

65

Short answer, NO. But you can share the venv build scripts.

  1. pip freeze all libraries to a requirements.txt file.

    pip freeze > requirements.txt
    
  2. Create the venv on each OS:

    python -m venv env
    source env/bin/activate
    pip install -r requirements.txt  # Install all the libs.
    

There are several reasons why venvs cannot be shared across OSes:

  1. Some packages contains C extensions, and the OS' .dlls are not compatible with each other.
  2. venvs contain scripts with hardcoded paths. Windows and Linux paths are different.
alex
  • 6,818
  • 9
  • 52
  • 103
Shuo
  • 8,447
  • 4
  • 30
  • 36
  • ok thanks Shuo! so, venv created form both OS must be in the same directory? (e.g D:\venv\test) And then operate from the rigth folder of each one? – Cristianjs19 Mar 11 '17 at 09:59
  • @Cristianjs19 No, if you build venv on each os, you can put it anywhere. But when share the venv folder, that requires the same path. – Shuo Mar 11 '17 at 10:02
  • Is possible to "migrate" a virtualenv created in Windows if the virtualenv cannot be activated( well with time and installing the same machine as W7, I would following the answer)? – Carmine Tambascia Apr 03 '19 at 13:02
  • 1
    a small addition in Windows `source env/bin/activate` should be chaged to `env\Scripts\activate` – Semen Oct 20 '21 at 11:17
  • Yeah this is actually really frustrating, why did they create a different structure for venv in windows and linux? Now my bash scripts that should run on either native machines or in git-bash are getting complicated, not to speak of developer instructions – SvenS Aug 18 '22 at 12:46