从父级目录中的另一个文件夹中的模块导入函数

发布于 2025-01-28 00:26:21 字数 652 浏览 1 评论 0原文

我一直在尝试解决这个问题几个小时。 这是我的文件夹结构。

/parent_folder
      main.py
      module1/
        script1.py
      module2/
        script2.py

script2.py只有此内部:

def subtract_numbers(x, y):
    return x - y

我希望Script1.py能够调用此功能。 我有:

from ..module2.script2 import subtract_numbers

result_subtraction = subtract_numbers(5, 5)
print(result_subtraction)

我得到Importerror:未知的父母软件包的尝试相对导入,

我在scrip1.py中的导入行中尝试了许多不同的排列,但是我遇到了相同的错误。我还必须注意,我有两个文件夹中的__ INT __。py文件。

我该如何确切地调用script2.py中的函数?

I have been trying unsuccessfully to solve this for hours now.
This is my folder structure.

/parent_folder
      main.py
      module1/
        script1.py
      module2/
        script2.py

script2.py has only this inside:

def subtract_numbers(x, y):
    return x - y

I want script1.py to be able to call this function.
I have:

from ..module2.script2 import subtract_numbers

result_subtraction = subtract_numbers(5, 5)
print(result_subtraction)

I get ImportError: attempted relative import with no known parent package

I have tried many different permutations in the import line in scrip1.py but i get the same error. I also have to note that i have __init__.py files in the two folders.

How exactly can i call the function in script2.py?

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

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

发布评论

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

评论(2

夏の忆 2025-02-04 00:26:21

相对导入不能比python呼叫的级别更高。因此,您的问题是您直接从module1目录调用script1.py。我猜是这样的事情:

user:/path/to/parent/module1$ python script1.py

因此,您需要从可以实际看到script.py2.py的级别中调用script1.py

首先,将您的相对导入更改为script1.py中的绝对导入:

from module2.script2 import subtract_numbers

然后,移至module1的父级目录那里的脚本(请注意提示中的位置):

user:/path/to/parent$ python -m module1.script1

Relative imports cannot go back to a higher level than the one from which the python call was originated. So, your problem here is that you are invoking script1.py directly from the module1 directory. I guess something like this:

user:/path/to/parent/module1$ python script1.py

So you will need to make your call to script1.py from a level where you can actually see script2.py.

First, change your relative import to absolute import in script1.py:

from module2.script2 import subtract_numbers

Then, move back to the parent directory of module1 and run the module as a script from there (note the location in the prompt):

user:/path/to/parent$ python -m module1.script1
穿透光 2025-02-04 00:26:21

这对我有用。

我将父目录的目录(/parent_folder)导出到pythonpath

export PYTHONPATH=$PYTHONPATH:/home/username/Desktop/parent_folder

然后,内部文件module1/script1.py,我更改了这一行:

from module2.script2 import subtract_numbers

现在我可以调用在Module2/ script2.py带有python script1.py,它将起作用。

我还必须注意,我到处都有__ INT __。

This is what worked for me.

I exported the directory of the parent directory (/parent_folder) to the PYTHONPATH.

export PYTHONPATH=$PYTHONPATH:/home/username/Desktop/parent_folder

Then, inside file module1/script1.py, i had this line changed:

from module2.script2 import subtract_numbers

Now i can call the script script1.py that calls the function declared in module2/script2.py with python script1.py and it will work.

I also have to note that i have __init__.py files everywhere (in the parent directory and both subfolders), but i am now sure if this is vital.

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