且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何确保python脚本使用虚拟环境?

更新时间:2023-11-09 20:32:10

在许多情况下,没有必要激活虚拟环境.通常,您可以在不激活虚拟环境的情况下从任何地方执行以下操作:

In many cases it's not necessary to activate a virtual environment. Typically you could do something like the following from anywhere without activating the virtual environment:

  • C:\path\to\venv\Scripts\python.exe -m pip somecommand
  • C:\path\to\venv\Scripts\python.exe different\path\to\script.py

此外,您可以将 python.exe 的绝对路径指定为 Python 脚本顶部的she-bang,然后直接执行脚本(例如双击),无需显式调用 Python.

Additionally you could specify the absolute path to the python.exe as a she-bang at the top of your Python script, and execute the script directly (for example with a double-click) without calling Python explicitly.

#!/path/to/venv/bin/python3

print("Hello world")

参考资料: