python进口的行为是通过直接路径运行解释器的行为
我运行了一个由虚拟环境中创建的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论