簡體   English   中英

如何使用“venv”更新 Python 虛擬環境以使用更新版本的 Python?

[英]How do I update a Python virtual environment with `venv` to use a newer version of Python?

我最近安裝了 Python 3.8.0 和 Python 3.7.4。

我有一些基於 v3.7.4 的虛擬環境(使用python -m venv <directory>創建。如何更新它們以使用 v3.8.0?

我是否需要創建一個新的虛擬環境並重新安裝依賴項、腳本等?


注意:有一些現有的問答(例如 this )處理較舊的virtualenv包/工具。 我特別詢問新的內置venv模塊,它是自 v3.3 以來 Python 的內置標准,與virtualenv有一些差異。

我猜你正在尋找的是--upgrade參數。

python -m venv --help
usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
            [--upgrade] [--without-pip] [--prompt PROMPT]
            ENV_DIR [ENV_DIR ...]

Creates virtual Python environments in one or more target directories.

positional arguments:
  ENV_DIR               A directory to create the environment in.

optional arguments:
  -h, --help            show this help message and exit
  --system-site-packages
                        Give the virtual environment access to the system
                        site-packages dir.
  --symlinks            Try to use symlinks rather than copies, when symlinks
                        are not the default for the platform.
  --copies              Try to use copies rather than symlinks, even when
                        symlinks are the default for the platform.
  --clear               Delete the contents of the environment directory if it
                        already exists, before environment creation.
  --upgrade             Upgrade the environment directory to use this version
                        of Python, assuming Python has been upgraded in-place.
  --without-pip         Skips installing or upgrading pip in the virtual
                        environment (pip is bootstrapped by default)
  --prompt PROMPT       Provides an alternative prompt prefix for this
                        environment.

您需要使用目標 python 版本運行它,例如在這種情況下:

python3.8 -m venv --upgrade <path_to_dir>

假設 python3.8 是您的 python 3.8.0 可執行文件的名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM