Search

25 June, 2018

How to work on a Python Project when modules versions needed are different? Use virtualenv



Gone are the days when you need to create a VM for working on a different set of tools.  People work on different projects which might need a special version of a module. (Or python itself) . The problem aggravates when  multiple users work on a single shared machine (Test environments or Build machines ) .Virtual Environments help solve problem . And its easy to use and share among fellow teammates.


Installation:


pip install virtualenv



Note: I am using 15.2.0 ver of vituralenv module and my system is Windows 10 64 bit.

(Windows OS) Creating a virtual environment:


virtualenv ProjectName


This will take some time . If you have only one installation of Python , then above step will use that Python version . Lets say you have two different python installations (2.7 and 3.4) . Then you can specify which python to be used to make this environment . For creating a version specific env, use this :


virtualenv -p C:\Python27\python.exe temp_project


Note :  My python executable file path is used as above to specify which python to be used.



After the VE has been created , you can see the below contents in that folder .

Files and folders after creating virtual env in Python

Now to activate the environment , browse to script folder inside the above temp_project folder. Type in command prompt:


Scripts\activate


Now you will see on you prompt, your virtaulenv name inside parenthesis. Example:


(temp_project) C:\User\temp_project


Now we can install all the modules that we need for our project using pip command


To deactivate,

Scripts\deactivate


You can generate a list of requirements from you installed modules:



pip freeze > requirements.txt



Other Important tips:

  • If you need to install using a wheel file, you should copy the .whl file inside Lib\site-packages and then use "pip install filename.whl" . 
  • It's a good practice to upgrade certain modules like (setuptools, pip, incremental) before you start installing other modules.        
*********************************************************************************

On Unix system , creating and activating a virtualenv has different approach

To create:

virtualenv -p /usr/bin/python3

Tip: Use "which python" or "which python3" to find the path of python.

To activate , type: 

source MyVirtualEnv/bin/activate

To deactivate:

deativate
                

No comments:

Post a Comment