- Posted on
- • Web Development
Installing Python and virtual environments (`venv`, `virtualenv`)
- Author
-
-
- User
- Linux Bash
- Posts by this author
- Posts by this author
-
Installing Python and Managing Virtual Environments in Linux Bash: A Comprehensive Guide for Web Developers
As a web developer delving into the Python ecosystem, setting up a proper development environment is critical for running and managing your projects efficiently. It’s essential to have control over the versions of Python and libraries your project uses, which is why understanding how to install Python and manage virtual environments is so vital.
In this guide, I'll walk you through installing Python on a Linux system, as well as setting up virtual environments using venv
and virtualenv
. This step-by-step tutorial will help ensure that all your projects are neatly organized and maintained, avoiding the "it works on my machine" syndrome.
Step 1: Installing Python
Using the Package Manager
Most Linux distributions come with Python installed, or they provide it through their package management system. Here’s how you can install Python using popular package managers:
On Ubuntu/Debian-based distributions:
Open your terminal and run:
sudo apt update
sudo apt install python3
On Fedora/Red Hat-based distributions:
For RHEL systems, you might need to enable the EPEL repository before installing Python:
sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
sudo dnf install python3
On openSUSE:
sudo zypper refresh
sudo zypper install python3
On Arch Linux:
sudo pacman -S python3
Verifying the installation
To verify that Python has been installed correctly, run the following command in your terminal:
python3 --version
This should output the version of Python installed on your system.
Step 2: Setting Up Virtual Environments
Virtual environments allow you to manage separate package installations for different projects. This means you can have different versions of Python packages for each project without them interfering with each other. We’ll look at two tools to create virtual environments: venv
(included in Python 3.3 and later) and virtualenv
(a more flexible, external tool).
Using venv
to Create a Virtual Environment
venv
is built into Python 3 and is the recommended way to create and manage virtual environments. Here’s how you can set it up:
Choose a directory where you want to place your virtual environment and switch to it:
mkdir myproject cd myproject
Run the following command:
python3 -m venv myenv
This command creates a directory called
myenv
in yourmyproject
directory. Inside, it includes a fresh Python interpreter and a separate directory for libraries.To activate the virtual environment:
source myenv/bin/activate
Your prompt should change, indicating that you are now working within the virtual environment. It typically prepends the name of the environment to your shell prompt.
To deactivate and return to the global Python interpreter:
deactivate
Using virtualenv
virtualenv
is a powerful alternative to venv
and can be used to create virtual environments with different Python versions. First, install virtualenv
if it’s not already installed:
pip install virtualenv
Create a new directory for your project and navigate into it:
mkdir myproject cd myproject
Create a virtual environment:
virtualenv myenv
Activate the virtual environment:
source myenv/bin/activate
To deactivate:
deactivate
Step 3: Managing Packages Within a Virtual Environment
While your virtual environment is activated, you can install packages using pip
. The packages will only be available within the virtual environment. For example:
pip install flask
This installs Flask within your virtual environment without affecting the global Python installation.
Conclusion
Setting up Python and managing virtual environments are crucial steps in maintaining project dependencies and ensuring that applications run reliably across different systems. By using venv
or virtualenv
, you can isolate and manage dependencies effectively, making your life as a web developer much easier and more productive.
Happy coding!
Further Reading
For further reading on managing Python environments and installation, consider the following resources:
Python Virtual Environments: A Primer
Real Python
This tutorial offers a deep dive into using virtual environments in Python, covering bothvenv
andvirtualenv
.The Hitchhiker’s Guide to Python: Virtual Environments
Python Guide
This guide provides practical advice on managing multiple Python projects using virtual environments.Managing Multiple Python Versions With pyenv
Pyenv GitHub
The official GitHub repository forpyenv
, a tool used for managing multiple Python versions.Python Packaging User Guide
Python.org
Official documentation that explains how to install packages with pip and manage them with virtual environments.Using Python Environments in VS Code
Visual Studio Code Documentation
Discusses how to use and manage Python environments in Visual Studio Code, enhancing productivity and project management.