python uses a virtual environment to install third-party modules and run python scripts

1. Create a virtual environment named env38 in the user's home directory

python3 -m venv env38

Create the directory after creating the virtual environment env38 

2. Activate the virtual environment

source ~/env38/bin/activate

 3. Install pyqt5

pip install pyqt5

 4. Write a simple python script

import sys
print(sys.path)

# 如果执行成功,没有任何错误提示,则表明环境搭建成功
from PyQt5 import QtWidgets

# 当然也可以查看PyQt版本
from PyQt5.QtCore import *
print(QT_VERSION_STR)

 5. Execute the script in a virtual environment

 6. Select the python interpreter in the virtual environment in VScode

  • In the above VScode, you cannot directly click the run button to run the script directly, otherwise an error will be reported that the pyqt5 module cannot be found.
  • There are wavy lines in the script indicating that PyQt5 cannot be parsed.

Solve the above two problems:

At this time, the wavy line alarm is gone. You can also click the run button to run it directly. When you do not install third-party modules, you can exit the virtual environment and run the python script. When installing third-party modules, activate the virtual environment.

  • By explicitly stating that the python interpreter in a virtual environment is used to run the python script (without activating the virtual environment):

  •  Run the script directly using python in the virtual environment (need to activate the virtual environment):

Guess you like

Origin blog.csdn.net/m0_46829545/article/details/132129336