简体   繁体   English

Python 虚拟环境“activate_this.py”实际上做了什么?

[英]What does Python Virtual Env "activate_this.py" actually do?

I'm working on a python CLI project that, unfortunately, is a little complex on the virtual environments side, as it has to deal with multiple ones.我正在开发一个 python CLI 项目,不幸的是,它在虚拟环境方面有点复杂,因为它必须处理多个。

In my quest to get my tool to work properly and robustly, I came across Virtual Env's "activate_this.py" file, which is generated inside the .venv/bin/ directory.为了让我的工具能够正常且稳健地工作,我遇到了 Virtual Env 的“activate_this.py”文件,该文件是在 .venv/bin/ 目录中生成的。 I thought this could be useful for my needs so I started experimenting with it, but I haven't truly understood what it is actually doing under the hood.我认为这可能对我的需求有用,所以我开始尝试它,但我还没有真正理解它实际上在做什么。

For example, it says that the script should be used when a file must be called from an existing python interpreter (which is my case), but the virtual environment must be activated.例如,它说当必须从现有的 python 解释器(这是我的情况)调用文件时应该使用脚本,但必须激活虚拟环境。

What I'm struggling to understand is: What exactly does activating a virtual environment on a given file mean?我正在努力理解的是:在给定文件上激活虚拟环境究竟意味着什么? (Since we're not changing the interpreter) (因为我们没有改变解释器)

Also, on the activate_this.py file there's this bit of code:此外,在 activate_this.py 文件中有这段代码:

# add the virtual environments libraries to the host python import mechanism
prev_length = len(sys.path)
for lib in "../lib/python3.9/site-packages".split(os.pathsep):
    path = os.path.realpath(os.path.join(bin_dir, lib))
    site.addsitedir(path.decode("utf-8") if "" else path)
sys.path[:] = sys.path[prev_length:] + sys.path[0:prev_length]

Does this mean that you can now import libraries that are installed on the virtual env you've just activated, even if they are not installed in the environment of the base interpreter?这是否意味着您现在可以导入安装在刚刚激活的虚拟环境中的库,即使它们没有安装在基本解释器的环境中?

You've got it.你明白了。 It rewrites your sys.path to expose the stuff installed in the virtual environment, and it cuts off access to the globally installed third-party libraries not in the virtual environment.它会重写您的sys.path以公开安装在虚拟环境中的内容,并切断对不在虚拟环境中的全局安装的第三方库的访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Windows上的fabfile中使用activate_this.py激活python虚拟环境 - Activate a python virtual environment using activate_this.py in a fabfile on Windows “conda activate”实际上并未激活虚拟环境 - 'conda activate' not actually activating a virtual env `python setup.py check`实际上做了什么? - What does `python setup.py check` actually do? IBM Cloud Functions -“无效的 virtualenv。Zip 文件不包括 activate_this.py” - IBM Cloud Functions - "Invalid virtualenv. Zip file does not include activate_this.py" Python产生:OSError:[WinError 193]%1不是有效的Win32应用程序,仅带有activate_this.py - Python produces: OSError: [WinError 193] %1 is not a valid Win32 application, but only with activate_this.py python print()函数实际上做了什么? - What does python print() function actually do? 蟒蛇。 这个表达式实际上是做什么的? - Python. What does this expression actually do? Python的“ BeautiulSoup()”函数实际上是做什么的? - Python 'BeautiulSoup()' function what does it actually do? ==运算符实际上对Python字典做了什么? - What does the == operator actually do on a Python dictionary? Python 中的 format() 实际上是做什么的? - What does format() in Python actually do?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM