[We often say that there are traps in this pycharm, the import of third-party libraries fails, look here! 】

Recently, some friends encountered the problem that although the python third-party library was installed, it was not successfully imported into pycharm.

Preface

For a long time, many friends who are new to Python have accidentally jumped into the [trap] of virtual environments and system environments.

Based on this issue, this article will talk about how to use the system environment and virtual environment in pycharm.

In pycharm, each project needs to specify a python environment when running, such as python3.7, python3.8, etc.

1. Does your project use a virtual environment?

When creating a project in pycharm, you need to select the python environment . The default is to create a virtual environment.

Many friends have not taken care of this step. It’s natural to create a virtual environment without even realizing it.

As shown below:

picture

After clicking it, the virtual environment is selected by default.

picture

After the virtual environment is successfully created, there will be a venv directory under the project.

picture

picture

If there is this directory under your project, it means you are using a virtual environment.

In pycharm, when executing the code of this project, third-party libraries in the virtual environment will be automatically used.

If it does not exist in the virtual environment, the import will fail.

So the problem encountered at the beginning of this article is actually for this reason.

Third-party libraries are installed in the local python environment, and when running the code, the virtual environment is used.

For Python beginners, don’t use a virtual environment, don’t use a virtual environment, don’t use a virtual environment!

Use the local system environment to learn python well first, and then consider using a virtual environment later.

2. Project environment switching - how to switch from virtual environment to local environment

So, what should students who have accidentally fallen into this [trap] want to switch the project from a virtual environment to a local Python environment?

In [File -> Settings -> project: your project name -> python interpreter], you can specify the python environment of the current project.

picture

picture

Clicking Show All will display all environments, as shown below. If the environment path contains [venv], it means a virtual environment

picture

We need to choose the local python environment instead of the virtual environment.

picture

After the environment switch is successful, the local python environment path will be displayed under [External Libraries].

picture

When running in pycharm, the local environment will be used to execute the code.

When writing python code, the imported packages are also imported from this python environment.

3. In pycharm, third-party library management in python virtual environment

Generally, when developing python projects, a virtual environment is used.

The purpose of the virtual environment is to isolate the python environment between projects and the python third-party libraries used by the projects.

If your project is currently in a virtual environment, how to enter the virtual environment to install a third-party library?

2 ways to install and uninstall third-party libraries in a virtual environment.

3.1) Install and uninstall in the terminal of pycharm.

Step 1: Change pycharm's Terminal from powershell to cmd.exe. (windows environment)

picture

In this case, we need to manually execute the command and enter the virtual environment.

If you replace windows powershell with cmd.exe, then after opening Terminal, you will automatically enter the virtual environment.

This way you don't have to switch every time.

Switching steps: [File - Settings - Tools - Terminal - Shell path - select the one with cmd.exe]

picture

Close the previously opened Terminal and re-open it. It will automatically switch to the virtual environment.

picture

Step 2: Use the pip command to install and uninstall.

picture

picture

picture

3.2) Install and uninstall third-party libraries in the project's interpreter configuration interface

Step one: Enter the project interpreter configuration interface.

Operation steps: [File - Settings - Project: your project name - Python Interpreter]

picture

Step 2: Click + to enter the third-party library installation interface.

picture

picture

To uninstall an installed package

picture

picture

Regarding the solution to the problem of slow installation or read timeout of third-party libraries:

You can use the following domestic sources when installing third-party libraries:

可以在安装第三方库时使用以下国内源:

阿里云 http://mirrors.aliyun.com/pypi/simple/ 

豆瓣(douban) http://pypi.douban.com/simple/ 

清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/ 

中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/ 

pip 安装命令为:

pip install XXXX -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com

end                                                                  

Guess you like

Origin blog.csdn.net/m0_58552717/article/details/132313489