-
Open a terminal window.
-
Navigate to the directory where you want to create the virtual environment. You can use the
cd
command to change directories. -
Run the following command to create a virtual environment with Python 3:
python3 -m venv venv_name
Replace venv_name
with the name you want to give to your virtual environment. For example:
python3 -m venv myenv
This command will create a directory with the specified name (e.g., myenv
) in your current location, and it will set up a new Python environment within that directory.
- Activate the virtual environment by running the following command:
source venv_name/bin/activate
Replace venv_name
with the name you used in step 3. After running this command, your terminal prompt should change to indicate that you are now working within the virtual environment.
-
You can now install packages and work with Python as if it's a fresh Python environment. Any packages you install will be isolated within this virtual environment and won't affect your system-wide Python installation.
-
To deactivate the virtual environment and return to your system's global Python environment, simply run:
deactivate