在不修改 sys.path 的情况下无法从根目录导入到子目录(Python 3.8)

发布于 2025-01-16 21:45:35 字数 1377 浏览 3 评论 0原文

我尝试过的

首先我要说的是,我已经阅读了大约 5 或 6 个与此相关的其他 stackoverflow 问题,并且已经对此进行了很多讨论。这是我发现和尝试的内容:

  1. “在其他导入之前执行 sys.path.append()。”

    问题:这可行,但违反了 PEP 标准实践,因此我宁愿找到一个不需要修改 sys.path 的解决方案。

  2. “使用 imp 库查找您的模块。”

    问题:该答案中概述的解决方案已被弃用,因为 importlib 是新的 imp,我不确定如何使用importlib 即使在查看文档之后也是如此。

  3. “在根目录中添加一个空的 __init__.py 文件。”

    问题:这不起作用。

我的问题的详细信息

这是我的文件结构:(

Project/
  ├ subdir/
  |   └ script.py
  ├ __init__.py
  └ module.py

请注意,我将空的 __init__.py 文件留在那里,以防需要。)

我正在尝试从 module.py 导入subdir/script.py

module.py 的内容:

def func():
    print('func was called')

subdir/script.py 的内容:

from module import func
func()

但是,当我在终端中运行 python3 script.py (在 Mac 上),我收到此错误消息:

Traceback (most recent call last):
  File "script.py", line 1, in <module>
    from module import func
ModuleNotFoundError: No module named 'module'

我希望有一个不需要修改 sys.path 的简单解决方案。

What I have tried

First let me say, I have read maybe 5 or 6 other stackoverflow questions related to this and many things have been said about this. Here's what I found and what I tried:

  1. "Do sys.path.append(<root dir>) before other imports."

    Issue: This works, but it's against PEP standard practice so I'd rather find a solution that does not require modifying sys.path.

  2. "Use the imp library to find your module."

    Issue: The solution outlined in that answer is deprecated as importlib is the new imp, and I'm not sure how to use importlib even after looking at the docs.

  3. "Add an empty __init__.py file in your root directory."

    Issue: This does not work.

Details of my problem

Here is my file structure:

Project/
  ├ subdir/
  |   └ script.py
  ├ __init__.py
  └ module.py

(Note that I left the empty __init__.py file in there just in case it's needed.)

I'm trying to import from module.py to subdir/script.py:

Contents of module.py:

def func():
    print('func was called')

Contents of subdir/script.py:

from module import func
func()

However, when I run python3 script.py in terminal (on Mac), I get this error message:

Traceback (most recent call last):
  File "script.py", line 1, in <module>
    from module import func
ModuleNotFoundError: No module named 'module'

I would appreciate a straightforward solution that does not require modifying sys.path.

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

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

发布评论

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

评论(2

撕心裂肺的伤痛 2025-01-23 21:45:36

解决您的问题的最简单方法可能是重组您的项目:

Project
   ├── script.py
   └── module
         ├── __init__.py
         └── module.py

Probably the easiest way to solve your problem is to restructure your project:

Project
   ├── script.py
   └── module
         ├── __init__.py
         └── module.py
怪我闹别瞎闹 2025-01-23 21:45:36

我通过设置 PYTHONPATH 解决了我的问题。就我而言,它适用于此。

PYTHONPATH=<root dir> python3 subdir/script.py

I solved my issue by setting PYTHONPATH. In my case, it works with this.

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