Installing Python and Setting Up Your First Virtual Environment: A Beginner’s Guide
Introduction
If you’re just getting started with Python development, you’ll quickly realize how important it is to manage your development environment. Virtual environments are essential to keep your projects organized and prevent dependency conflicts. In this article, I’ll guide you through installing Python and setting up your first virtual environment.
Step 1: Installing Python
First things first—let’s get Python installed on your system.
Windows:
Visit the official Python website: python.org.
Navigate to the “Downloads” section and choose the latest stable release for Windows.
During installation, ensure you check the option “Add Python to PATH” (this is crucial).
Click “Install Now”, and Python will install along with the
pip
package manager.
macOS:
- Open the Terminal and type:
brew install python
If you don’t have Homebrew installed, follow the instructions here to get it first.
- Verify the installation by typing:
python3 --version
Linux:
Most Linux distributions come with Python pre-installed. You can verify by running:
python3 --version
If it’s not installed, use the following command based on your package manager:
sudo apt update
sudo apt install python3 python3-pip
Step 2: Verifying Your Python Installation
After installation, open your terminal or command prompt and type:
python --version
or
python3 --version
You should see a message displaying the version number. If you don’t see this, ensure that Python has been added to your system’s PATH.
Step 3: Installing venv
(Virtual Environment Tool)
Python 3.x comes with a built-in tool called venv
for creating virtual environments. This isolates your Python projects and their dependencies, so they don’t interfere with each other.
Check if venv
is Installed
You can verify if venv
is available by typing:
python -m venv --help
If you don’t see a help message, you’ll need to install the venv
module.
Installing venv
on Linux:
sudo apt install python3-venv
Step 4: Creating Your First Virtual Environment
Now let’s create your first virtual environment!
- Navigate to the project directory where you want the environment to live. For example:
cd my-python-project
- Run the following command to create a virtual environment:
python -m venv venv
This will create a folder named venv
in your project directory, which will hold the environment files.
Step 5: Activating the Virtual Environment
Once the virtual environment is created, you need to activate it.
On Windows:
.\venv\Scripts\activate
On macOS/Linux:
source venv/bin/activate
After activating, your command prompt will show (venv)
before the directory path, indicating that the environment is active.
Step 6: Installing Packages Inside the Virtual Environment
When your virtual environment is activated, you can install packages using pip
without affecting your global Python installation. For example:
pip install requests
The installed packages will now be contained within the venv
folder. You can list installed packages by running:
pip freeze
Step 7: Deactivating the Virtual Environment
When you’re done working, you can deactivate the virtual environment by typing:
deactivate
This will return you to your system’s default Python environment.
Conclusion
Setting up a virtual environment is a crucial step in managing your Python projects effectively. Now that you know how to install Python and create your first virtual environment, you’re ready to start building projects without worrying about package conflicts.
Stay tuned for more beginner-friendly Python guides!
Subscribe to my newsletter
Read articles from Likhith SP directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Likhith SP
Likhith SP
My mission is to help beginners in technology by creating easy-to-follow guides that break down complicated operations, installations, and emerging technologies into bite-sized steps.