将导入从外部方法中传递到脚本

发布于 2025-01-30 01:35:10 字数 824 浏览 3 评论 0 原文

我有一个棘手的问题,因此甚至很难形容它。 假设我有这个脚本,我们将其称为 master

#in master.py
import slave as slv
def import_func():
    import time
slv.method(import_func)

我想在 slave.py.py 中确保方法,看起来像这样:

#in slave.py
def method(import_func):
    import_func()
    time.sleep(10)

实际上像我导入时间软件包。目前,它不起作用,我认为这是因为该导入仅存在于 import_func()的范围中。 请记住,游戏的规则是:

  1. 我无法在 slave.p y外部方法中
  2. 导入任何内容,我需要通过 import_func()在Master.py
  3. 该过程必须适用于方法内部的导入数量。换句话说,方法不知道它将收到多少进口,但仍需要工作。
  4. 该过程需要为任何可能的进口工作。因此,诸如 pyforest 之类的选项不合适。

我知道从理论上可以通过 ementlib 来完成,但是我更喜欢一个更直接的想法,因为如果我们有很多具有不同的“'as”标签的进口,它将变得非常乏味,并且与 ementlib 。 我知道这是一个古怪的问题,但我真的很想知道是否有可能。谢谢

I have kind of a tricky question, so that it is difficult to even describe it.
Suppose I have this script, which we will call master:

#in master.py
import slave as slv
def import_func():
    import time
slv.method(import_func)

I want to make sure method in slave.py, which looks like this:

#in slave.py
def method(import_func):
    import_func()
    time.sleep(10)

actually runs like I imported the time package. Currently it does not work, I believe because the import stays exists only in the scope of import_func().
Keep in mind that the rules of the game are:

  1. I cannot import anything in slave.py outside method
  2. I need to pass the imports which method needs through import_func() in master.py
  3. the procedure must work for a variable number of imports inside method. In other words, method cannot know how many imports it will receive but needs to work nonetheless.
  4. the procedure needs to work for any import possible. So options like pyforest are not suitable.

I know it can theoretically be done through importlib, but I would prefer a more straightforward idea, because if we have a lot of imports with different 'as' labels it would become extremely tedious and convoluted with importlib.
I know it is kind of a quirky question but I'd really like to know if it is possible. Thanks

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疏忽 2025-02-06 01:35:10

您可以在主文件中做什么:

#in master.py
import slave as slv
def import_func():
    import time
    return time
slv.method(import_func)

现在使用从属文件中的时间返回值:

#in slave.py
def method(import_func):
    time = import_func()
    time.sleep(10)

为什么必须这样做?是因为应用程序的堆栈。当 import_func()在从slave.py上调用时,它将在堆栈上导入库。但是,当函数终止时,所有堆栈数据都会从内存中释放出来。因此,图书馆将被垃圾收集器释放和收集。

通过从 import_func()返回 time ,您可以保证一旦函数终止执行,它将继续存在于内存中。

现在,导入更多模块?简单的。返回一个内部带有倍数模块的列表。或者也许是简单访问的字典。那是一种方法。


[edit]使用字典和Omportlib将多个导入传递给slave.py:

master.py:slave.py

import test2 as slv
import importlib

def master_import(packname, imports={}):
    imports[packname] = importlib.import_module(packname)

def import_func():
    imports = {}
    master_import('time', imports)

    return imports

slv.method(import_func)

#in slave.py
def method(import_func):
    imports = import_func()
    imports['time'].sleep(10)

这样,您可以在Master.py上实际导入所需的任何模块,使用 master_import()函数,然后将它们传递给从脚本。

检查 this 关于如何使用EmentLib的答案。

What you can do is this in the master file:

#in master.py
import slave as slv
def import_func():
    import time
    return time
slv.method(import_func)

Now use time return value in the slave file:

#in slave.py
def method(import_func):
    time = import_func()
    time.sleep(10)

Why would you have to do this? It's because of the application's stack. When import_func() is called on slave.py, it imports the library on the stack. However, when the function terminates, all stack data is released from memory. So the library would get released and collected by the garbage collector.

By returning time from import_func(), you guarantee it continues existing in memory once the function terminates executing.

Now, to import more modules? Simple. Return a list with multiples modules inside. Or maybe a Dictionary for simple access. That's one way of doing it.


[Edit] Using a dictionary and importlib to pass multiple imports to slave.py:

master.py:

import test2 as slv
import importlib

def master_import(packname, imports={}):
    imports[packname] = importlib.import_module(packname)

def import_func():
    imports = {}
    master_import('time', imports)

    return imports

slv.method(import_func)

slave.py:

#in slave.py
def method(import_func):
    imports = import_func()
    imports['time'].sleep(10)

This way, you can literally import any modules you want on master.py side, using master_import() function, and pass them to slave script.

Check this answer on how to use importlib.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文