SwadhyAI Notes

All Python notes

Python Virtual Environment 🌐

Why Virtual Environments?

A virtual environment is a tool that helps to keep dependencies required by different projects separate by creating isolated python virtual environments for them.

✨ Creating a Virtual Environment

To create a virtual environment, decide upon a directory where you want to place it, and run the venv module as a script with the directory path:

Terminal
python3 -m venv myenv

This will create the myenv directory if it doesn't exist, and also create directories inside it containing a copy of the Python interpreter, the standard library, and various supporting files.

🚀 Activating the Environment

Before you can start installing or using packages in your virtual environment you'll need to activate it.

Windows

Terminal
myenv\Scripts\activate

Unix or MacOS

Terminal
source myenv/bin/activate

🛑 Deactivating

If you want to switch projects or otherwise leave your virtual environment, simply run:

Terminal
deactivate

✨ Pro Tip

Always use a virtual environment for your projects to avoid version conflicts between libraries!