• Active Topics 

Python Virtual Environments: virtualenv

Including Cython, Jython, IronPython, PyPy, Django framework, and interpreters: Ipython, IPython Jupyter/Notebook, CPython


Post Reply
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5211
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#1

Virtual environments, virtualenv, is a way of setting different Python environments for different projects. Python is used for many different purposes, usually in association with various dependencies, each needed for achieving some specific functionality.

Suppose you have multiple projects and all rely on a single package but each project depends on a specific version of that package. So, if you upgrade the package globally it could break all other projects/software that depend on the package's previous version. Due to this reason, you would not dare to do that, instead you would like each of the projects to have an isolated environment together with their package dependencies and the specific versions they need. This is the point where the use of virtual environments, virtualenv, comes in.

This short tutorial shows how to set and use separate Python environments.
  1. Make sure you have pip installed, otherwise install it
  2. sudo apt-get install python-pip #Python 2
  3. sudo apt-get install python3-pip #Python 3
  4. #Or upgrade pip
  5. pip install --upgrade pip
  6. #Install virtualenv
  7. pip install virtualenv
  8. #Make directory to keep all separate environments in one place
  9. mkdir Environments
  10. #Make the first virtualenv
  11. virtualenv project_venv1
  12. #Activate the virtualenv
  13. source project_venv1/bin/activate  
  14. #Or
  15. . project_venv1/bin/activate #Try which pip or which python to see if they are inside the newly created environment
  16. #Install packages inside the new virtualenv, project_venv1, here we install numpy and scipy
  17. pip install numpy
  18. pip install scipy
  19. #Type "pip list" to see which packages were installed in this new environment
  20. pip list
  21. #Export the local dependencies in the virtualenv to a file requirements.txt
  22. pip freeze --local > requirements.txt
  23. #Check that all the dependencies have been moved to requirements.txt
  24. ls
  25. #List dependencies in the requirements.txt file
  26. cat requirements.txt
  27. #Get out of virtualenv
  28. deactivate #Type which python to confirm that the python listed is in the global environment
  29. #Delete the created virtualenv if you no longer need it
  30. rm -rf project_venv1/ #Note that the requirements.txt file is still availbale
  31. #You can specify a specific version of software you want for a particular virtual environment project. Suppose we want to use a global Python 2.7 inside the virtualenv project_venv2
  32. virtualenv -p /usr/bin/python2.7 project_venv2
  33. #Activate this new environment to use with Python 2.7
  34. source project_venv2/bin/activate
  35. #Check if the exported global Python 2.7 is currently in this new environment
  36. which python
  37. #Check which version of Python you are using inside the virtual environment, project_venv2
  38. python --version
  39. #Install the packages in the requirements.txt file to the current virtualenv, project_venv2
  40. pip install -r requirements.txt
  41. #Check if these packages are installed by typing
  42. pip list
  43. #Again, you can deactivate the virtualenv and switch to using global packages
  44. deactivate

Conclusion

Virtual environments are meant to be environments for separating packages, dependencies and the versions that you will use from project to project.
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5211
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#2

If you no longer need the Python virtual environment/virtualenvwrapper, you can delete it by simply deactivating it and get rid of the associated applications by recursively removing it:

Code: Select all

deactivate 
sudo rm -rf venv
rmvirtualenv venv # for virtualenvwrapper
You can however remove all currently installed packages, but keep the virtual environment itself. For example, if you have virtualenvwrapper installed:

Code: Select all

$virtualenv --clear path_to_your_venv

You can remove all the dependencies by recursively uninstalling all of them and then delete the venv.

Code: Select all

source venv/bin/activate
pip freeze > requirements.txt # If used pip to install dependencies, pip freezes a dependency list and keep at the top level directory
pip uninstall -r requirements.txt -y
rm -rf venv/
deactivate
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5211
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#3

pip is a Python package installer, recommended for installing Python packages which are not available in the Debian archive. It can work with version control repositories (currently only Git, Mercurial, and Bazaar repositories), logs output extensively, and prevents partial installs by downloading all requirements before starting installation.

On Debian, pip is the command to use when installing packages for Python 2, while pip3 is the command to use when installing packages for Python 3.
0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
Post Reply
  • Similar Topics
    Replies
    Views
    Last post

Return to “Python Programming”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 0 guests