46

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?

1
  • Follow this answer to learn how to produce identical virtualenvs in the two OSes (ignore the change from python 2 to 3) stackoverflow.com/a/41390096/267540 Commented Mar 11, 2017 at 9:31

1 Answer 1

73

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.
Sign up to request clarification or add additional context in comments.

5 Comments

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 No, if you build venv on each os, you can put it anywhere. But when share the venv folder, that requires the same path.
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)?
a small addition in Windows source env/bin/activate should be chaged to env\Scripts\activate
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 😕

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.