How to use the same virtualenv (python) in Windows and Linux?

1

I started using Windows and Linux recently in the same PC (installed in different partitions respectively and a third one for common files) but it turns out that the environments created in Windows are generated with the "Scripts" folder, and in Linux the equivalent it would be the "bin" folder.
The problem here is that the files in these folders are not compatible for both OS. for example, "activate" content in bin (created in Linux) can not be executed in Windows, and vice versa, the "activate" of Scripts (created in Windows) does not run in Linux.

How can it be done to work on the same virtualenv from both SOs?

    
asked by Cristianjs19 05.03.2017 в 21:11
source

1 answer

5

You can not do it as you suggest it. But what you need to understand is that the environment is recreated in each operating system. It is the same because it offers you the same capabilities, but from the perspective of each operating system.

Look, when I say that it recreates is because both meet the same requirements. When you do pip freeze you get the environment that you use in an OS can be used in any other ... but be careful, we talk about the environment , not the specific binary files.

In fact, there are very few Python modules that can only be used in * nix ... right now I only remember the readline library that can not be used in Windows, although there are alternatives. And, of course, the drivers that are linked with binary libraries.

The requirements file

The key to replicating a virtual environment is the requirements file. This is a simple text file that contains all the packages installed in the current work environment (always think of virtual environments). It is created in a very simple way, executing this order

$ pip freeze --local > requisitos.txt

As already explained, it contains the packages and indicates the installed version. A requirements file looks like this:

dj-database-url==0.4.1
Django==1.10.3
django-allauth==0.28.0
django-braces==1.10.0
django-configurations==2.0
django-coverage-plugin==1.3.1
django-crispy-forms==1.6.1
django-debug-toolbar==1.6
django-environ==0.4.0

The purpose of this file is to replicate the work / production environment on any system, whichever it is .

How it works

What happens is that a virtual environment is a set of configurations and environment variables that isolate a Python executable so that it only searches for and installs packages in a particular directory and, unless indicated, Ignore the packages installed in the system .

In this way, I can have a virtual environment that uses Django 1.6 and another that uses Django 1.10 in the same operating system and I just have to activate with the one I want to work with.

And it works also in the case you pose. When you are in Windows you create a virtual environment that installs the packages in, say, %appdata% . When you create a virtual environment on Linux, use the ~/.virtualenv/ directory, for example.

So, in either of your two systems you just have to do:

pip install -r requisitos.txt

and pip will be responsible for searching, downloading, installing and configuring the indicated modules.

With the environment activated, you can work with your project wherever you are. Let's say that in Windows you work in d:/proyectos/compartido and linux in /opt/proyectos/compartido . Since the files are the same, you just have to activate the virtual environment to make it work.

  

Important

     

Since the key is the file re requirements, it is very important that you remember to update it every time you install or update a package. It does not matter what system you're on , but always remember to run pip install -r ... on the other.

In this sense, you would not use a version control like Git for a better control of your project.

Conclusion

In summary: you can not and should not use the same directories . Fortunately, virtual environments can be reproduced and work virtually the same on any operating system.

Alternative

You can use Vagrant. Configured correctly, you can specify the guest's route and in the case you present, the guest can be the same for both host systems. And the image may be in your third partition.

    
answered by 05.03.2017 / 21:42
source