-->

python virtual environment setup in ubuntu

Python is a open source software. It has thousands of third-party packages developed by developers all over the world. All these packages were maintained by PiPy.

For developing complex application we need to install many third-party libraries based on the requirement. All these packages were installed in system level.
If we are working on two projects in parallel  one project is dependent on python2.7 and other is dependent on python3.5.

After completion of development the installed packages are no more required.
And they may slowdown the working speed of the system. To avoid such conditions we use python virtual environment.

We use system package "python-pip" for virtualenv.
sudo apt-get install python-pip

"virtualenv" is a third-party python package, it provides python virtual environment.
  1. Setup virtualenv python

  2. pip install virtualenv
    virtualenv project_env
    # for python3
    virtualenv -p python3 project_env 
  3. activate env to use virtualenv

  4. source project_env/bin/activate
    
  5. Install required packages

  6. pip install requests
    
  7. Un-Install packages from env

  8. pip remove requests
    
  9. Get list of all installed packages

  10. pip freeze
    
  11. Save list of required packages with version

  12. pip freeze > requirements.txt
    
  13. Install saved requirements

  14. pip install -r requirements.txt
    
  15. Deactivate the virtualenv when you finish work

  16. deactivate
    

Advantages using python virtual environment

  • We can experiment with new packages
  • We can have list of required packages for future reference.
  • We can maintain clean technical requirements for project.

Architecture

python virtual environment
Virtual environment architecture

 

Buy a product to Support me