python进口的行为是通过直接路径运行解释器的行为

发布于 2025-02-08 06:24:27 字数 1264 浏览 1 评论 0原文

我运行了一个由虚拟环境中创建的Python 3.8解释器

python3.8 -m venv〜/my_venv

重要:我不通过

源〜/my_venv/bin/activate

我直接运行python解释器,因此:

/home/user/my_venv/bin/python3.8 my_script.py

,在这种情况下,我的所有虚拟环境库都包含在sys.path中,而sys.prefix设置为/home/home/user/user/my_venv。看起来我不需要明确激活环境即可使用它,只需从其路径中运行解释器即可。

问题是: 它是标准的和有记录的行为,还是未记录的内容,并且可能在未来版本中更改,甚至可以在其他OS安装上进行更改?


我可以找到一些东西: https:///docs.python.org/ 3.10/使用/unix.html#与Python相关的Paths and Files

  • Python使用前缀(sys.prefix)查找库目录。

https://docs.pypython.org/3.10/library/sys。 html#sys.prefix

如果虚拟环境有效,则该值将在site.py中更改为指向虚拟环境。

这也可以解释某些东西,但是很明显什么意味着“虚拟环境有效”?我没有激活环境usind“源〜/my_venv/bin/activate”,我刚刚从其目录中运行了一个python解释器。它是如何得到的,它在虚拟环境中,并且需要将前缀更改为特定目录?在此虚拟Env Directory中,我在与Python解释器中没有发现任何“ site.py”。

也许我错过了Python文档中的某些内容?

I run a Python 3.8 interpreter that is in virtual environment created by

python3.8 -m venv ~/my_venv

Important: I don't activate this environment by

source ~/my_venv/bin/activate

Instead I run python interpreter directly, like this:

/home/user/my_venv/bin/python3.8 my_script.py

And in this case I have all virtual environment libraries is included in sys.path, and sys.prefix is set to /home/user/my_venv. Looks like I don't need to explicitly activate environment to use it, just run an interpreter from it's path.

The question is:
Is it a standard and documented behavior, or something non-documented and could change in future versions, or even if on different OS installation?


I could find something:
https://docs.python.org/3.10/using/unix.html#python-related-paths-and-files

  • python use prefix (sys.prefix) to find libraries directory.

https://docs.python.org/3.10/library/sys.html#sys.prefix

If a virtual environment is in effect, this value will be changed in site.py to point to the virtual environment.

It is also explain something, but it isn't obvious what means "a virtual environment is in effect"? I didn't activated environment usind "source ~/my_venv/bin/activate", I have just run a python interpreter from it's directory. How did it get, that it is in virtual environment, and it needs to change prefix to a specific directory? I found no any "site.py" in the along with python interpreter in this virtual env directory.

Maybe there is something in python documentation, that I missed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文