Python environment configuration, creation, startup and shutdown of virtual environment and common pip commands in VSCode

Tool preparation

Install Visual Studio CodeInstall
PythonInstall plug-ins in VScode: Python, Jupyter

virtual environment

In order not to affect the original installation package, it is generally necessary to create a new virtual environment to write Python code.

  1. Create a new empty folder and open the folder with VSCode, such as E:/python project
  2. Open Terminal Ctrl++Shift`
    Insert image description here
  3. Create a virtual environment:
python -m venv .venv
  1. Activate and start the virtual environment
.venv/Scripts/activate								#方法一
& e:/4.python项目/.venv/Scripts/Activate.ps1			#方法二

Insert image description here
Activation may fail due to the above situation. The reason for this code is the powershell security protocol.
Solution: Open Windows PowerShell (administrator), enter set-executionpolicy remotesigned, Enter, Enter, and then enter "y"
as shown in the figure:
Insert image description here
Insert image description hereEnter the activation command again.
Insert image description here

  1. pip install xxx package:
python -m pip install xxx			#方法一
pip install xxx						#方法二
  1. Close virtual environment
deactivate

pip common commands

ipykernel library installation

If you are new to Python, it will be more convenient to write code in jupyter, but you need to install the ipykernel library first. The command is as follows:

pip install ipykernel -i https://mirrors.bfsu.edu.cn/pypi/web/simple/

Add a few Python sources:

https://mirrors.aliyun.com/pypi/simple/ #阿里云
https://pypi.tuna.tsinghua.edu.cn/simple/ #清华大学
https://mirrors.bfsu.edu.cn/pypi/web/simple/ #中国科学技术大学
https://pypi.doubanio.com/simple/ #豆瓣
https://mirrors.cloud.tencent.com/pypi/simple/ 
https://mirrors.163.com/pypi/simple/
https://pypi.python.org/simple #官网

How to use third-party libraries when migrating Python projects:

1. Use pip freeze > file name in the original environment, which will generate a file.
2. Use pip install -r file name (the file generated in the first step) in the new environment to download and install it, so that it will not Missing dependent libraries and perfectly migrating the project environment.
example:

pip freeze     # 列出当前项目已经安装的库
pip freeze > requirements.txt     # 把第三方库保存成一个txt文本
pip install -r requirements.txt     # 安装txt文本中的第三方库
pip install -i https://pypi.mirrors.ustc.edu.cn/simple/ -r requirements.txt

pip installation and upgrade related commands

If pip is missing in python (not adding python -m before upgrading the pip command will cause pip to disappear), you can call the following command to reinstall it

python -m ensurepip --upgrade

List the libraries that need to be upgraded in the current environment:

pip list --outdated

The upgrade command of pip in Python is separate:

python -m pip install --upgrade pip

Upgrade commands provided by Python itself:

pip install --upgrade 库名

If you need to batch upgrade third-party libraries to the latest version:

import subprocess

# pip显示需要更新的python列表
com_list_o = 'pip list -o' 
# 执行命令并返回结果
p = subprocess.Popen(com_list_o, shell=True, stdout=subprocess.PIPE)
# 取命令返回结果,结果是一个二进制字符串,包含了我们上面执行pip list -o后展现的所有内容
out = p.communicate()[0]
# 二进制转utf-8字符串
out = str(out, 'utf-8')

# 切出待升级的包名, 并存入列表
need_update = []
for i in out.splitlines()[2:]:
    need_update.append(i.split(' ')[0])

# 执行升级命令,每次取一个包进行升级,pip只支持一个包一个包的升级
for nu in need_update:
    com_update = 'pip install -i https://mirrors.aliyun.com/pypi/simple/ -U {py}'.format(py=nu)
    print("执行命令:", com_update)
    subprocess.call(com_update)
    print("----------{com} 执行结束-----------\n".format(com=com_update))


print("检查更新情况:")
if subprocess.call(com_list_o) == 0:
    print("已全部更新完毕!")
else:
    print("未能全部更新,剩下的请手动删除重新下载!")


Use pip to download the specified version of the package:

pip install 包名==版本号
//例如
pip install numpy==1.24.2
pip install numpy # 默认是下载最新的包
pip install numpy -i https://mirrors.bfsu.edu.cn/pypi/web/simple/ #镜像安装

Introduction to Python related packages

1. numpy

References:

VSCode configuration Python tutorial
Python environment configuration, virtual environment creation and pip common commands in VSCode_How to use pip in vscode

Guess you like

Origin blog.csdn.net/qq_41361442/article/details/130990700