Welcome to the world of Python! In this beginner's guide, we'll walk you through the steps of installing Python, creating virtual environments, and freezing requirements. Learning and understanding the role of virtual environments changed the way I start developing a Python project and made sure I don't make a mess of my Python install.
A virtual environment is a personalised playground for each of your Python projects. It keeps them separate, helps manage dependencies, ensures consistency, and simplifies project cleanup. It's like having your own private space to play and explore without any interference from other projects or your base Python installation.
Step 1: Installing Python
Visit the official Python website at www.python.org.
Look for the "Downloads" section and choose the version of Python suitable for your operating system.
Download the Python installer and run it.
During installation, make sure to check the box that says "Add Python to PATH" (Windows) or "Install for all users" (macOS and Linux).
Follow the prompts and complete the installation.
Step 2: Creating Virtual Environments
Virtual environments help keep your Python projects organized and isolated. Here's how to create one:
Open your command prompt or terminal.
Navigate to or create a directory where you want to create your project (e.g.,
cd Documents/MyProject
).Run the following command to create a virtual environment:
For Windows:
python -m venv venv
For macOS and Linux:
python3 -m venv venv
You can name the environment something more meaningful than 'venv'. I keep it as that because Github will ignore it by default when using the Python template.
- Activate the virtual environment:
For Windows:
venv\Scripts\activate
For macOS and Linux:
source venv/bin/activate
Step 3: Selecting the Interpreter in VS Code
VS Code allows you to select the Python interpreter for your project. Make sure you have the Python extension installed.
Here's how you can choose the Python interpreter:
Open the folder where your virtual environment is located.
Open the command palette by selecting "View" --> "Command Palette" from the top menu or using the shortcut
Ctrl+Shift+P
(Windows/Linux) orCmd+Shift+P
(macOS).In the command palette, search for "Python: Select Interpreter" and select it.
A list of available interpreters will be displayed. Choose the one corresponding to your virtual environment (e.g. "venv").
VS Code will remember your selection and use it for running and debugging Python code within the project.
Great! You've successfully configured the interpreter for your project in VS Code.
Step 4: Freezing Requirements:
Freezing requirements helps ensure consistent dependencies for your project. Follow these steps:
- With your virtual environment activated, install the necessary packages using the
pip
package manager:
pip install streamlit
- Once you have all the required packages, create a "requirements.txt" file that lists them by running this command:
pip freeze > requirements.txt
- You now have a "requirements.txt" file that captures all the packages and versions needed for your project. If you open the file you'll see a list of packages and the version.
Why freezing dependencies is a good idea?
Freezing requirements offers several benefits:
1. Reproducibility:
By documenting the exact package versions, you ensure that anyone working on your project can recreate the same development environment.
2. Collaboration:
When collaborating with others, sharing the "requirements.txt" file allows them to set up the same environment effortlessly. Everyone involved can work on the project with confidence, knowing they have the necessary dependencies.
3. Deployment:
When deploying your application to a production environment or sharing it with others, the "requirements.txt" file serves as a recipe for setting up the correct environment. It helps ensure that the required packages and versions are installed, reducing the chances of compatibility problems.
Best Practices:
It's a good practice to create a virtual environment for each project to keep dependencies separate.
Keep your virtual environment active while working on a project to ensure you're using the correct dependencies.
Regularly update your virtual environment and installed packages by running:
pip install --upgrade pip
pip install --upgrade -r requirements.txt
- Consider using version control, such as Github, to manage your code and include the "requirements.txt" file in your repository for easy sharing. Also, remember to use the Github template for Python so it automatically ignores your virtual environment._
I wrote this to have a guide for when I get a fresh PC install to get up and running quickly. Hopefully, this is useful and easy to follow.
Here I am on LinkedIn, please also like and subscribe if you enjoy the read and to force me to keep writing :)
Top comments (0)