How to create a python virtual environment in Ubuntu?

To create a Python virtual environment on Ubuntu you can use venvthe module . Here are the steps:

  1. First, make sure you have Python installed. You can check if Python is installed python3 --versionby If you see a version number, Python is already installed. If not, you need to install Python first.

  2. Install venvthe module . On Ubuntu, you can use the following command to install:

sudo apt-get install python3-venv
  1. Create a virtual environment. First, navigate to the directory where you wish to create your virtual environment, then run the following command (where envis the name of your virtual environment):
python3 -m venv env
  1. Activate the virtual environment. Run the following command:
source env/bin/activate

You should now be in a new Python virtual environment. You'll notice that your command prompt has changed and should now contain the name of the virtual environment (in this case it is env). You can now start installing any required Python packages.

  1. If you want to exit the virtual environment, just type:
deactivate

NOTE: When you use pippackages , they are only available in that environment. This is a great way to avoid package version conflicts and keep your global environment clean.

Guess you like

Origin blog.csdn.net/m0_57236802/article/details/131249438