02-014 - Additional Reading - Setting Up A Virtual Environment
02-014 - Additional Reading - Setting Up A Virtual Environment
Additional Reading
Introduction
In this guide, we will walk you through the process of setting up a virtual environment
for your Python project using either the built-in venv module or the external virtualenv
package. Virtual environments isolate your project's dependencies, providing a clean
and self-contained environment.
We’ll explore the generation of a requirements.txt file to document and manage your
project's dependencies. Keeping this file up-to-date is crucial for ensuring a
reproducible development environment. By following these steps, you will be
well-equipped to initiate and maintain a Python project with best practices in mind,
fostering readability, consistency, and ease of collaboration.
Using venv
Follow these steps to use the venv module (built into Python 3.3 and newer versions) to
create a virtual environment for your project:
2. Use the cd command to go to the directory where you want to create your virtual
environment, for example:
cd your_path/to/your/project
3. Run the following command to create a virtual environment. Replace the second
venv with the name you want to give your virtual environment.
a. On Windows, use:
venv\Scripts\activate.bat
source venv/bin/activate
After activation, your terminal prompt should change to show that you are now
in the virtual environment. You should see something like this:
(.venv) C:\Users\your_path\to\your\project>
6. When you're done working in your virtual environment, you can deactivate it.
a. On Windows, use:
venv\Scripts\deactivate.bat
source venv/bin/deactivate
Using virtualenv
The virtualenv package is an external package we can use to create a virtual
environment for your project.
cd your_path/to/your/project
3. Run the following command to create a virtual environment. Replace venv with
the name you want to give your virtual environment.
virtualenv venv
a. On Windows, use:
venv\Scripts\activate.bat
source venv/bin/activate
After activation, your terminal prompt should change to show that you are now
in the virtual environment. You should see something like this:
(.venv) C:\Users\your_path\to\your\project>
6. When you're done working in your virtual environment, you can deactivate it.
a. On Windows, use:
venv\Scripts\deactivate.bat
source venv/bin/deactivate
Take note
cryptography==42.0.2
bcrypt==2.26.0
3. If you want to include only the packages needed for your project (excluding
development dependencies), you can use the pip freeze --exclude-editable
option:
This command will exclude packages installed in editable mode from the
requirements.txt file. Editable installations are often used during development
with packages installed in "editable" mode, e.g., using the command:
The “.” at the end of the command refers to the current directory, indicating that
the package in editable mode should be installed from the current project
directory. Excluding it ensures that only production dependencies are listed.
Summary
In conclusion, this guide has equipped you with the knowledge to kickstart your
Python project development in a structured and efficient manner. By embracing virtual
environments and the generation of a requirements.txt file, you will establish a solid
foundation for building scalable, maintainable, and organised applications.
Incorporating these best practices not only enhances the clarity and readability of your
code but also streamlines collaboration and ensures a smooth development
experience. As you continue on your coding journey, the skills and techniques outlined
here will contribute to the success of your Python projects, fostering a robust and
sustainable development workflow. Remember that, if you get stuck, you can contact
your mentor for help.