~ 3 min read

Creating and Managing Python Virtual Environments for Development

By: Adam Richardson
Share:

Creating and Managing Python Virtual Environments for Development

Introduction to Python Virtual Environments

Virtual environments are essential when working with multiple Python projects, as they help you manage dependencies and avoid conflicts. This article provides insights on using virtual environments in Python to effectively manage project resources and dependencies, enhancing your development workflow.

Properties, Parameters, and Usage

Python virtual environments help isolate installed packages and their specified versions, ensuring that projects with differing dependencies run smoothly.

To create and manage virtual environments, first, decide between the two widely-used tools: virtualenv and venv. In general, virtualenv works well with both Python 2 and Python 3, while venv is exclusive to Python 3.3 and later versions.

1. Setting up virtualenv

To install virtualenv, run:

pip install virtualenv

2. Setting up venv

If you’re using Python 3.3 or later, venv is included in Python’s core library, making installation unnecessary.

3. Creating a virtual environment

For virtualenv:

virtualenv myenv

For venv:

python3 -m venv myenv

Replace myenv with your desired environment name.

4. Activating the virtual environment

Activate your environment to use it. The activation differs on Windows and UNIX systems:

For virtualenv in UNIX:

source myenv/bin/activate

For virtualenv in Windows:

myenv\Scripts\activate

For venv in UNIX:

source myenv/bin/activate

For venv in Windows:

myenv\Scripts\activate.bat

5. Deactivating the virtual environment

To deactivate the virtual environment, simply type:

deactivate

Simplified Real-Life Example

Imagine that we are building a web application using Flask. We want to ensure that our project has an isolated environment that contains all dependent packages.

First, create a directory for the project, and navigate into it:

mkdir my_flask_app
cd my_flask_app

Next, create and activate a virtual environment using virtualenv:

virtualenv venv
source venv/bin/activate

With the environment active, install Flask:

pip install Flask

Create your simple Flask app, app.py:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

if __name__ == '__main__':
    app.run()

Run the application:

python app.py

After development, deactivate the environment:

deactivate

Complex Real-Life Example

Consider a more complex project with multiple developers using different operating systems. The project involves Django, PostgreSQL, and Celery, with dependencies specified in a requirements.txt file.

Create the project folder:

mkdir my_celery_app
cd my_celery_app

Set up a virtual environment:

python3 -m venv myenv

Activate the environment (UNIX):

source myenv/bin/activate

Or, for Windows:

myenv\Scripts\activate.bat

Install necessary dependencies from requirements.txt:

pip install -r requirements.txt

Create your Celery application, celery_app.py:

from celery import Celery

app = Celery('my_celery_app', broker='pyamqp://guest@localhost//')

@app.task
def add(x, y):
    return x + y

Finally, initiate a Celery worker:

celery -A celery_app worker --loglevel=info

After finishing your work, release the virtual environment:

deactivate

Personal Tips

  1. Be diligent in maintaining your virtual environments, always employing them when working with Python projects. Isolating dependencies can save valuable time debugging conflicts.
  2. Make it mandatory to provide a requirements.txt file for every project. This enables easy replication of package dependencies in various environments.
  3. Stay consistent with naming conventions for virtual environments; adopting a standardized approach helps simplify management.
  4. To avoid potential version conflicts, consider using tools such as virtualenvwrapper to enhance virtualenv or pipenv as an alternative to pip and virtual environment management.
Share:
Subscribe to our newsletter

Stay up to date with our latest content - No spam!

Related Posts