Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using same library with different version across virtual environment

I am working on a project in python which have dependencies in multiple modules.

As an example say my main module uses another 2 modules namely module_1 which needs library lib_version_1.5 and module_2 which uses the same library_but_version_1.8 .

Both module_1 and module_2 is running fine, now I am creating another module combining these two.

So how can I use virtual environment so that I can combine these two with all required libraries (different versions) ? Is it possible to get different version of the same library installed within a project having different sub modules?

like image 633
Sreejithc321 Avatar asked Oct 19 '15 07:10

Sreejithc321


People also ask

Can I use the same virtual environment for different projects?

Virtual environments are meant to keep things isolated from each other. If one project is a dependency of the other one, then they have to be installed in the same environment. If two projects have dependencies that conflict with each other, then they have to be installed in different environments.

Can virtual environment be shared?

If you want to share your virtualenv with a lab partner, you just need to allow read-execute access to the Envs directory and any files below that. All that's stored in there is the software you installed (e.g., with pip ), so it's OK to open up this directory for others to access.

Can I have two versions of Python installed?

If you wish to use multiple versions of Python on a single machine, then pyenv is a commonly used tool to install and switch between versions. This is not to be confused with the previously mentioned depreciated pyvenv script. It does not come bundled with Python and must be installed separately.


1 Answers

Note that the python namingspace is quite powerful. Hope this small Example will help you.

The basic theory is that add the same libs with different versions into the python's class path and make them be different names so that there will not be overriding.

import os, sys
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'libs'))

The above code will insert the third-part libs which under the directory of 'libs' that under your project's base directory.

Suppose that I need pycrypto with version 2.4.1 and 2.6.1, use pip install pycrypto==2.6.1 -t ./libs and ip install pycrypto==2.4.1 -t ./libs to install the two versions into a sub-directory under your project. Modify their name after you have installed one, otherwise it will override the existed one.

Modify their names to Crypto241 and Crypto261 seprately. What my project looks like

Let's say I will have two modules(ModuleWIthCrypto241, ModuleWithCrypto261) which will import pycrypto 2.4.1 and 2.6.1 seprately.

ModuleWithCrypto261 with code:

def getCryproVersion(baseDir):
    import os, sys
    sys.path.insert(0, os.path.join(baseDir, 'libs'))
    import Crypto261
    return Crypto261.__version__

ModuleWIthCrypto241 with code:

def getCryproVersion(baseDir):
    import os, sys
    sys.path.insert(0, os.path.join(baseDir, 'libs'))
    import Crypto241
    return Crypto241.__version__

Almost the same, just import the different version of pycrypto.

Then we have another script which try to get the two different version of pycrypto.modulewithdiffversion.py

What it looks like:

import os, sys

from com.x import ModuleWIthCrypto241
from com.x import ModuleWithCrypto261
if __name__ == '__main__':
    baseDir = os.path.dirname(os.path.abspath(__file__))
    print ModuleWIthCrypto241.getCryproVersion(baseDir)
    print ModuleWithCrypto261.getCryproVersion(baseDir)

Execute the main method, it will print out the two versions of the pycrypto:

2.4.1
2.6.1

Hope this will help you!

like image 183
lowitty Avatar answered Oct 18 '22 01:10

lowitty