This readme provides instructions on how to set up a Python virtual environment using python -m venv and install packages from a requirements.txt file within that virtual environment.
Make sure you have the following installed on your system:
- Python (Version 3.x recommended)
-
Open your terminal or command prompt.
-
Navigate to the directory where you want to create your virtual environment.
-
To create a new Python virtual environment, run the following command:
python -m venv .venv
This will create a virtual environment named
.venvin the current directory. -
Activate the virtual environment:
On Windows:
.venv\Scripts\activate
On macOS and Linux:
source .venv/bin/activateAfter activation, you'll notice that your command prompt changes to show the name of the virtual environment (e.g.,
(.venv)).
-
Make sure you have a
requirements.txtfile in your project directory that lists all the required packages and their versions.For example, the
requirements.txtfile should look like this:package1==1.0.0 package2==2.1.0 package3>=3.2.1 -
With the virtual environment activated, use
pipto install the packages listed in therequirements.txtfile:pip install -r requirements.txt
This command will install all the packages specified in the
requirements.txtfile into your virtual environment. -
Once the installation is complete, you can start working with your Python project, and it will use the packages installed within the virtual environment.
After you have finished working on your project and want to deactivate the virtual environment, run the following command in the terminal:
deactivateThis will return your command prompt to its original state.
Congratulations! You have now successfully created a Python virtual environment using python -m venv and installed packages from the requirements.txt file within that virtual environment. You are ready to develop your Python project with a clean and isolated environment. Happy coding!