Installing every project requirement packages to global environment is difficult to manage and once you are done with your project these installed packages are redundant unless you are going to use them for almost every other project you start. Keeping a track of these project specific requirements also become difficult once these requirements start piling up in your global environment. Not every requirement version is will be compatible with other package versions for eg., some projects may be built using Django with version < 1.7 and maybe another project is working on the latest version like current 1.11. These create compatibility issues for one of the projects and since you can not install both versions at a time, you will have to keep re-installing Django according to the project you are currently working on. Compatibility issues are just the start with the same package but different versions.
This article is a continuation of the previous topic of web development and deployment on Raspberry Pi but can be used even for other Python projects as well. If you are interested in web development I would recommend going through the previous article too.
2. Installing Virtual Environment
Well, there is a way you can easily manage these packages and keep track of them too. All we need to do is create a virtual environment for every project separately. These virtual environments are like separate environment spaces on your disk drive which can be created, modified and deleted without messing up your global environment. We would use pip which is a Python package management system just like npm if you are a Nodejs developer. To install pip, download pip installation file and run it via Python.
$ wget https://bootstrap.pypa.io/get-pip.py
$ python get-pip.py
You may have to give sudo access for installation. Once you are done with installing pip we can now install virtual environment via a package called virtualenv. This package will be installed in the global environment so that it can be accessed from anywhere. Installing is easy and is as follows.
$ sudo pip install virtualenv
Virtualenv documentation is simple and can be found here. Well, virtualenv as a standalone package is great but activating an environment is time-consuming as you have to type out the whole virtual environment path every single time you have to use it. It is surely inconvenient for me and as well as other developers. Do not worry, the solution to this is also simple and would make use of a wrapper to ease our work. The following steps will help you install Virtualenv wrapper via pip.
$ sudo pip install virtualenvwrapper
The wrapper needs some variables to be exported everytime you start your terminal for their working. Just add the following lines at the end of the file of your users .bashrc file using any text editor, in this case, vim.
$ vim ~/.bashrc
# Virtual Environment
Save the file and execute it by the following command so that you can start using the commands made available by the wrapper or just re-open your terminal.
$ source ~/.bashrc
Create a new virtual environment simply by the following command and once the virtual environment is created it will be auto activated for first use.
$ mkvirtualenv <custom_env_name>
To activate and deactivate a virtual environment just type the following commands respectively and start installing your project requirements. You need to activate your virtual environment every time in the terminal before running your project to fulfill your project dependencies and work as intended.
$ workon <custom_env_name>
3. Installing project requirements
Once you have your virtual environment setup and running, you can start installing packages via pip command line or pip install requirement file where all the project requirements are stated in a <requirement_file>.txt file and each line represent a new requirement.
$ pip install django // OR
$ pip install -r <requirement_file>.txt
Don't have a requirement file and don't know to create one? Don't worry just simply start installing your packages in your projects virtual environment and once you are done or have reached minimum packages you want to install with the project just use the below command to auto-generate a requirement file for your project.
$ pip freeze > /path/to/project/requirements.txt
This requirements file will not only help you manage and install required packages separately from your global environment but will also be very helpful if you are working on a group project with multiple collaborators. You can find more documentation of using virtual environment wrapper here. The next part of the article will include deploying your Python-based web application on your Raspberry Pi 2B or greater and will be out soon. The post will be updated as soon it is published.