Skip to content

Python Virtual Environments

Edward Baitsewe edited this page Feb 23, 2025 · 3 revisions

Python Virtual Environments

A quick guide to running basic commands in pipenv.

Managing your application's dependencies in development is best handled in a virtual environment. This prevents different versions of a package clashing, and ensures that each app has the correct version of a package and dependency load.

Virtual Environment Basics

To check if your Pipenv virtual environment is running, try the following methods:

1. Check if Pipenv shell is active

Run:

echo $VIRTUAL_ENV
  • If it prints a path (e.g., /home/user/.local/share/virtualenvs/myproject-xxxx), Pipenv is running.
  • If it's empty, Pipenv is not active.

Alternatively, you can try:

pipenv --venv
  • If the command returns a path, the environment exists.
  • If you see No virtualenv has been created, run pipenv shell to start it.

2. Check if Python is using Pipenv's virtual environment

Run:

which python

or on Windows:

where python
  • If it points to a path inside .venv or virtualenvs/, Pipenv is running.
  • If it points to the system Python (/usr/bin/python or C:\Python), Pipenv is not active.

3. Deactivate Virtual Environment

If you are in the wrong virtual environment you may exit the current virtual environment.

Run:

exit

or

deactivate

4. Activate The Correct Virtual Environment

Navigate to the correct project directory

If you're not already there, run a command to navigate to the correct directory:

cd ~/OneDrive/Coding\ Projects/docker-django-demo

Activate the correct virtual environment

If you’re using pipenv, run:

pipenv shell

5. Check Installed Dependencies

To check the dependencies installed in the virtual environment.

Check installed packages (if using Pipenv)

Run:

pip list

If it shows only system packages and not the ones from your Pipfile.lock, Pipenv might not be running.

6. Check Installed Dependencies

If Pipenv is not running, start it with:

pipenv shell

or run a command inside Pipenv:

pipenv run python

7. Install Dependencies

Install dependencies (if needed)
If you get a ModuleNotFoundError: No module named 'dotenv', install missing packages: if using pipenv:

pipenv install

8. Run Django Server

Run

python manage.py runserver

These command should resolve any issues! 🚀


Check Installed Packages

To check which packages are installed in your Pipenv virtual environment, use one of the following methods:

1. List Installed Packages

pipenv graph
  • Shows a dependency tree of installed packages.

2. Show Detailed Installed Packages

pipenv run pip list
  • Displays a flat list of installed packages and their versions.

3. Check Installed Packages in Pipfile

cat Pipfile
  • Shows declared dependencies (but not their installed versions).

4. Check Installed Packages in Pipfile.lock

cat Pipfile.lock

or

pipenv lock -r
  • Lists exact versions of installed dependencies.

5. Verify if a Specific Package is Installed

pipenv run pip show package_name

Example:

pipenv run pip show django
  • Displays details like version, location, dependencies, etc.

Let me know if you need more help! 🚀


Installing A Package

To install packages using Pipenv, follow these steps:

1. Install a Package

pipenv install package_name

Example:

pipenv install django
  • This installs django and adds it to your Pipfile.

2. Install a Package Only for Development

pipenv install package_name --dev

Example:

pipenv install black --dev
  • This installs black only for development (it won't be installed in production).

3. Install Multiple Packages at Once

pipenv install requests numpy pandas

4. Install a Specific Version

pipenv install django==5.0.7
  • Installs Django version 5.0.7.

5. Install Packages from a requirements.txt File

pipenv install -r requirements.txt

6. Install All Dependencies from Pipfile.lock

pipenv install
  • Installs all dependencies listed in Pipfile.lock.

7. Uninstall a Package

pipenv uninstall package_name

Example:

pipenv uninstall django

Let me know if you need more help! 🚀