pycharm installs virtual environment

pycharm installs virtual environment

complete process

new project

1. Create a new project

2. Open Settings

3. Enter the project->python interpreter->

4. Choose to add a virtual environment

5. Configuration location, interpreter (ie python version)

6. Check whether the entry is successful

old project

1. Open the project

2...Others are the same as above

3. Install dependency packages through requirements.txt

a brief introdction

The main reason for using a virtual environment in PyCharm is to provide an independent Python environment for your project, which has the following advantages:

  1. Isolate dependencies: When you use different Python libraries and dependencies in one project, dependencies between different projects may conflict. Using a virtual environment avoids this, allowing each project to have its own Python environment.
  2. Manage package versions: When you use many Python dependent libraries in a project, the versions of these dependent libraries may be incompatible, which will cause runtime errors. Using a virtual environment can help you manage the dependent library versions of different projects in a unified way, avoiding This situation.
  3. Flexibility: You can use different versions of Python in different virtual environments, which is very useful, especially when you need to upgrade or fall back to a specific Python version.

After installing the virtual environment, you can install the corresponding Python packages and dependencies in the virtual environment and use them in PyCharm, thus avoiding conflicts and version issues. Using virtual environments is a good habit and can help you better manage your Python development environment.

Precautions

New project specific steps

1. First open a project, enter the setting interface (ctrl+alt+S), and then enter the project->python interpreter directory

insert image description here

2. Choose to add a virtual environment and configure the interpreter version

insert image description here

3. Enter the terminal to check whether you have entered the virtual environment

insert image description here

4. If you don't see venv, you can do it manually (I simulate it in cmd)

Open cmd in the project root directory

insert image description here

Enter the following command and press Enter

venv\Scripts\activate

to enter the virtual environment
insert image description here

The specific steps of the old project

The previous operation is the same as above

1. When entering an existing project, old projects often configure a requirements.txt file to quickly install dependent packages
insert image description here

2. Enter the following command in the terminal

pip install -r requirements.txt -i  https://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn

Notice:

in

-r: indicates specifying a dependency package file

-i + url: Indicates the source of the specified installation package

–trusted-host + domain name: Indicates trust in this package source

Because the official package source server is abroad, the installation speed is very slow. So we need to configure a domestic mirror source by ourselves

The following mirror sources are commonly used:

Alibaba Cloud
http://mirrors.aliyun.com/pypi/simple/

University of Science and Technology of China
https://pypi.mirrors.ustc.edu.cn/simple/

Douban http://pypi.douban.com/simple/

Tsinghua University
https://pypi.tuna.tsinghua.edu.cn/simple/

University of Science and Technology of China
http://pypi.mirrors.ustc.edu.cn/simple/


3. The following interface appears, indicating that the execution is successful

insert image description here

4. If the following interface appears, the dependent package installation is successful

insert image description here

Summarize

So far, the operation of configuring the virtual environment in pycharm has been described.

Guess you like

Origin blog.csdn.net/ekcchina/article/details/130481867