Working with lightweight virtual environments
Throughout this book, we will be working with different frameworks and libraries, and therefore, it is convenient to work with virtual environments. We will work with the lightweight virtual environments introduced in Python 3.3 and improved in Python 3.4. However, you can also choose to use the popular virtualenv
(https://pypi.python.org/pypi/virtualenv) third-party virtual environment builder or the virtual environment options provided by your Python IDE.
You just have to make sure that you activate your virtual environment with the appropriate mechanism when it is necessary to do so, instead of following the step explained to activate the virtual environment generated with the venv
module integrated in Python. You can read more information about PEP 405 Python Virtual Environment that introduced the venv
module at https://www.python.org/dev/peps/pep-0405.
Tip
Each virtual environment we create with venv
is an isolated environment and it will have its own independent set of installed Python packages in its site directories. When we create a virtual environment with venv
in Python 3.4 and greater, pip is included in the new virtual environment. In Python 3.3, it was necessary to manually install pip after creating the virtual environment. Notice that the instructions provided are compatible with Python 3.4 or greater, including Python 3.5.x. The following commands assume that you have Python 3.5.x installed on macOS, Linux, or Windows.
First, we have to select the target folder or directory for our virtual environment. The following is the path we will use in the example for macOS and Linux. The target folder for the virtual environment will be the PythonREST/Django
folder within our home directory. For example, if our home directory in macOS or Linux is /Users/gaston
, the virtual environment will be created within /Users/gaston/PythonREST/Django
. You can replace the specified path with your desired path in each command.
~/PythonREST/Django
The following is the path we will use in the example for Windows. The target folder for the virtual environment will be the PythonREST/Django
folder within our user profile folder. For example, if our user profile folder is C:\Users\Gaston
, the virtual environment will be created within C:\Users\gaston\PythonREST\Django
. You can replace the specified path with your desired path in each command.
%USERPROFILE%\PythonREST\Django
Now, we have to use the -m
option followed by the venv
module name and the desired path to make Python run this module as a script and create a virtual environment in the specified path. The instructions are different depending on the platform in which we are creating the virtual environment.
Open a Terminal in macOS or Linux and execute the following command to create a virtual environment:
python3 -m venv ~/PythonREST/Django01
In Windows, execute the following command to create a virtual environment:
python -m venv %USERPROFILE%\PythonREST\Django01
The preceding command doesn't produce any output. The script created the specified target folder and installed pip by invoking ensurepip
because we didn't specify the --without-pip
option. The specified target folder has a new directory tree that contains Python executable files and other files that indicate that it is a virtual environment.
The pyenv.cfg
configuration file specifies different options for the virtual environment and its existence is an indicator that we are in the root folder for a virtual environment. In OS and Linux, the folder will have the following main sub-folders—bin
, include
, lib
, lib/python3.5
and lib/python3.5/site-packages
. In Windows, the folder will have the following main sub-folders—Include
, Lib
, Lib\site-packages
, and Scripts
. The directory trees for the virtual environment in each platform are the same as the layout of the Python installation in these platforms. The following screenshot shows the folders and files in the directory trees generated for the Django01
virtual environment in macOS:
The following screenshot shows the main folders in the directory trees generated for the virtual environments in Windows:
Tip
After we activate the virtual environment, we will install third-party packages into the virtual environment and the modules will be located within the lib/python3.5/site-packages
or Lib\site-packages
folder, based on the platform. The executables will be copied in the bin
or Scripts
folder, based on the platform. The packages we install won't make changes to other virtual environments or our base Python environment.
Now that we have created a virtual environment, we will run a platform-specific script to activate it. After we activate the virtual environment, we will install packages that will only be available in this virtual environment.
Run the following command in the terminal in macOS or Linux. Note that the results of this command will be accurate if you don't start a different shell than the default shell in the terminal session. In case you have doubts, check your terminal configuration and preferences.
echo $SHELL
The command will display the name of the shell you are using in the Terminal. In macOS, the default is /bin/bash
and this means you are working with the bash shell. Depending on the shell, you must run a different command to activate the virtual environment in OS or Linux.
If your Terminal is configured to use the bash
shell in macOS or Linux, run the following command to activate the virtual environment. The command also works for the zsh
shell:
source ~/PythonREST/Django01/bin/activate
If your Terminal is configured to use either the csh
or tcsh
shell, run the following command to activate the virtual environment:
source ~/PythonREST/Django01/bin/activate.csh
If your Terminal is configured to use either the fish
shell, run the following command to activate the virtual environment:
source ~/PythonREST/Django01/bin/activate.fish
In Windows, you can run either a batch file in the command prompt or a Windows PowerShell script to activate the virtual environment. If you prefer the command prompt, run the following command in the Windows command line to activate the virtual environment:
%USERPROFILE%\PythonREST\Django01\Scripts\activate.bat
If you prefer the Windows PowerShell, launch it and run the following commands to activate the virtual environment. However, notice that you should have scripts execution enabled in Windows PowerShell to be able to run the script:
cd $env:USERPROFILE PythonREST\Django01\Scripts\Activate.ps1
After you activate the virtual environment, the command prompt will display the virtual environment root folder name enclosed in parenthesis as a prefix of the default prompt to remind us that we are working in the virtual environment. In this case, we will see (Django01
) as a prefix for the command prompt because the root folder for the activated virtual environment is Django01
.
The following screenshot shows the virtual environment activated in a macOS El Capitan terminal with a bash
shell, after executing the previously shown commands:
As we can see in the preceding screenshot, the prompt changed from Gastons-MacBook-Pro:~ gaston$
to (Django01) Gastons-MacBook-Pro:~ gaston$
after the activation of the virtual environment.
The following screenshot shows the virtual environment activated in a Windows 10 Command Prompt, after executing the previously shown commands:
As we can notice from the preceding screenshot, the prompt changed from C:\Users\gaston\AppData\Local\Programs\Python\Python35
to (Django01) C:\Users\gaston\AppData\Local\Programs\Python\Python35
after the activation of the virtual environment.
Tip
It is extremely easy to deactivate a virtual environment generated with the previously explained process. In macOS or Linux, just type deactivate
and press Enter. In a Windows command prompt, you have to run the deactivate.bat
batch file included in the Scripts folder (%USERPROFILE%\PythonREST\Django01\Scripts\deactivate.bat
in our example). In Windows PowerShell, you have to run the Deactivate.ps1
script in the Scripts
folder. The deactivation will remove all the changes made in the environment variables.