程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Configure a virtual environment with Python

編輯:Python

使用Python配置虛擬環境

    • 前言
    • 1 創建虛擬環境
      • 1.1 打開命令行
      • 1.2 確定Python版本
      • 1.3 創建虛擬環境
    • 2 配置虛擬環境
      • 2.1 激活虛擬環境
      • 2.2 下載依賴包
    • 3 查看虛擬環境
    • 4 選擇虛擬環境
    • 參考資料

Take note updates as you learn,歡迎指正.

前言

針對不同的任務,will be applied differently Python解釋器,Install different dependencies.Want to configure different environments on the same computer,為了方便管理,可以創建不同的虛擬環境.常用的工具包括 Anaconda 和 Virtualenv,How to use can refer to the blog post:virtualenv的介紹及基本使用(所有命令解釋)和 conda常用命令:安裝,更新,創建,激活,關閉,查看,卸載,刪除,清理,重命名,換源,問題.
This article focuses on direct usePython創建虛擬環境.

1 創建虛擬環境

1.1 打開命令行

### Windows系統 ###
win + R
輸入cmd
### Linux系統 ###
Ctrl + Alt + T

1.2 確定Python版本

在命令行中輸入Python,確定當前Python版本.
When a computer has more than one installedPython版本,在Windows系統中,默認調用 環境變量 the top onePython版本.Which one is required for a virtual environmentPython版本,Just move its environment variables to the top.

1.3 創建虛擬環境

# 跳轉到目標目錄下
d:
cd d:\venv
# 創建虛擬環境
### Windows系統 ###
python -m venv 環境名稱
### Linux系統 ###
python3 -m venv 環境名稱

2 配置虛擬環境

2.1 激活虛擬環境

### Windows系統下 ###
# First jump to the environment folderScripts中
cd d:\venv\環境名稱\Scripts
# 激活當前環境
.\activate
### Linux系統下 ###
# First jump to the environment folderbin中
cd venv\環境名稱\bin
# 激活當前環境
source .\activate

2.2 下載依賴包

The download speed of foreign sources is relatively slow,可能出現鏈接超時.It can be downloaded from domestic mirror sources.

pip install Depends on the package name -i 地址

You can also add domestic mirror sources to config 中,之後使用 pip install' The command will be downloaded from this address first.

# 添加鏡像源
pip config set global.index-url 鏡像源地址
# 查看目前正在使用的源
pip config list
# 刪除源
pip config unset global.index-url

3 查看虛擬環境

激活環境後,使用 pip list You can view which packages are installed in the current environment.

4 選擇虛擬環境

VS Code 通過配置 settings.json 文件來選擇Python編譯器.詳細流程可以參考溫柔且上進c的博文.

  1. Ctrl + Shift + P 打開命令交互面板;
  2. 可以通過 Python: Select Interpreter 選擇,也可以通過 settings 打開 settings.json 文件;
  3. will be in a virtual environment Scripts\python.exe 添加到 python.pythonPath 中;
  4. 重啟 VS Code.
"python.pythonPath": "D:\\venv\\環境名稱\\Scripts\\python.exe"

Pycharm It is relatively simple to directly select the virtual environment folder Scripts\python.exe Python解釋器即可.

參考資料

溫柔且上進c. 解決問題:VScode使用python虛擬環境(圖文並茂版). SCDN博客.
Drowning Yang. vscode設置Python虛擬環境. CSDN博客.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved