简体   繁体   English

如何在 Virtualenv 的解释器启动时执行 Python 代码?

[英]How to execute Python Code on Interpreter Startup in Virtualenv?

I want to execute code after the python interpreter has started.我想在 python 解释器启动后执行代码。

We use virtualenv and up to now we had a file called sitecustomize.py which got executed during interpreter start up.我们使用 virtualenv 并且到目前为止我们有一个名为 sitecustomize.py 的文件,它在解释器启动期间执行。

The sitecustomize.py was part of our project. sitecustomize.py 是我们项目的一部分。 We use the Django definition of this term: It is a small python module which only holds config and nearly no code: Django's Definition of "Project"我们使用这个术语的 Django 定义:它是一个小的 python 模块,它只保存配置,几乎没有代码: Django 对“项目”的定义

Unfortunately some linux distros (Ubuntu) provide a global sitecustomize, and our per virtualenv sitecustomize does not get loaded.不幸的是,一些 linux 发行版 (Ubuntu) 提供了全局站点自定义,并且我们的每个 virtualenv 站点自定义没有被加载。

Question问题

How to run Python code on interpreter startup in a virtualenv?如何在 virtualenv 中在解释器启动时运行 Python 代码?

This code should be executed even if the interactive interpreter gets started.即使交互式解释器启动,也应该执行此代码。

Goal vs Strategy目标与策略

I don't care if this hook is called "sitecustomize" or different :-)我不在乎这个钩子是叫“sitecustomize”还是不同的:-)

An addition to @guettli's answer: you can even make a .pth file a part of your package's distribution, so when it is installed it will make some code run on python startup, and when it is uninstalled, this code will no longer run. @guettli 答案的补充:您甚至可以将.pth文件作为包分发的一部分,因此当它安装时,它会在 python 启动时运行一些代码,当它被卸载时,此代码将不再运行。

Example package:示例包:

  • startup.pth
  • setup.py

Contents of startup.pth : startup.pth内容:

import sys; print('Success!!')

Contents of setup.py : setup.py内容:

from setuptools import setup

setup(
    name='pth_startup_example',
    data_files=[
        ('.', ['startup.pth'])
    ]
)

After creating these files, run pip install .创建这些文件后,运行pip install . in the same directory with the files.在与文件相同的目录中。 That should install startup.pth in your root python directory, and you should see Success!!这应该在你的python 根目录中安装startup.pth ,你应该看到Success!! printed every time your interpreter runs.每次解释器运行时都会打印。 To undo that, run pip uninstall pth_startup_example .要撤消该操作,请运行pip uninstall pth_startup_example

You can add this to an existing package, or make a package like this a dependency of a different package.您可以将其添加到现有包中,或者使这样的包成为不同包的依赖项。

You can use a pth file like explained in this answer:您可以使用此答案中解释的 pth 文件:

https://stackoverflow.com/a/52555465/633961 https://stackoverflow.com/a/52555465/633961

A pth file gets loaded before the interpreter executes the first line of its input.在解释器执行其输入的第一行之前加载一个 pth 文件。

Use usercustomize .使用用户usercustomize

On some linux distros a global sitecustomize exists, on some not.在某些 linux 发行版上,存在全局 sitecustomize,有些则没有。

This can lead to confusing behaviour.这可能会导致令人困惑的行为。

No linux distro provides a usercustomize.没有 linux 发行版提供用户自定义。

See site查看网站

... After this, an attempt is made to import a module named usercustomize, which can perform arbitrary user-specific customizations, if ENABLE_USER_SITE is true. ...此后,尝试导入名为 usercustomize 的模块,如果 ENABLE_USER_SITE 为真,它可以执行任意用户特定的自定义。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM