2023-12-30: Virtual Environment#
Virtual env#
Creating a virtual environment in a specific folder allows you to isolate the dependencies of a project from the system-wide Python installation. Here’s a general guide on how to create a virtual environment in a folder:
Using venv#
(built-in module in Python 3.3 and later):
Navigate to the desired folder:
cd path/to/your/project/folder
Create a virtual environment:
python -m venv venv
This command will create a virtual environment named
venvin the current directory.Activate the virtual environment:
On Windows:
.\venv\Scripts\activate
On Unix or MacOS:
source venv/bin/activate
After activation, your command prompt or terminal prompt should change to indicate that you are now working within the virtual environment.
Install dependencies: Once the virtual environment is activated, you can use
pipto install the required packages for your project:pip install package_name
Deactivate the virtual environment:
deactivate
This command will return you to the global Python environment.
Using virtualenv:#
If you are using an older version of Python or prefer to use virtualenv, you can follow these steps:
Install
virtualenv(if not already installed):pip install virtualenv
Navigate to the desired folder:
cd path/to/your/project/folder
Create a virtual environment:
virtualenv venvActivate the virtual environment:
On Windows:
.\venv\Scripts\activate
On Unix or MacOS:
source venv/bin/activate
Install dependencies: Once the virtual environment is activated, you can use
pipto install the required packages for your project:pip install package_name
Deactivate the virtual environment:
deactivate
Choose the method that best suits your needs or the one you are more comfortable with. Virtual environments are an excellent practice to keep project dependencies isolated and manageable.