python没有模块从另一个文件夹导入.py时发现错误

发布于 2025-01-22 10:37:22 字数 928 浏览 3 评论 0原文

我遵循a 非常接受的答案,这对我不起作用。 这是我所拥有的: 文件夹结构:

demo文件夹

“在此处输入图像描述”

fold1

​img src =“ https://i.sstatic.net/ktire.png” alt =“在此处输入图像说明”>

另外,__ INIT __。py是一个空文件。没有什么。

目标:import customfunction.py来自demofold1 in fold2

code in mainfile.py < /代码>:

import pandas as pd
import Demo.customfunctions

输出:

ModuleNotFoundError: No module named 'Demo'

I have followed a wildly accepted answer, which did not work for me.
Here is what I have:
Folder structure:

Demo folder

enter image description here

Fold1

enter image description here

Fold2

enter image description here

Also, the __init__.py is an empty file. Has nothing in it.

Objective: import customfunction.py file from Demo or Fold1 into Fold2

Code in MainFile.py:

import pandas as pd
import Demo.customfunctions

Output:

ModuleNotFoundError: No module named 'Demo'

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

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

发布评论

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

评论(1

画中仙 2025-01-29 10:37:22

您所需要的只是当前工作目录,PythonPath环境变量的正确组合以及脚本的路径。

如果您当前的工作目录是下载,则可以运行demo.fold2.mainfile模块而无需修改pythonpath。 IE

> python -m Demo.Fold2.MainFile

或您可以将demo \ fold2 \ mainfile.py.py文件运行,如果将pythonpath设置为当前工作目录。即,

> :: relative PYTHONPATH
> set PYTHONPATH=.
> python Demo\Fold2\MainFile.py
>
> :: or absolute PYTHONPATH
> set PYTHONPATH=c:\absolute\path\to\Downloads
> python Demo\Fold2\MainFile.py

如果您当前的工作目录是demo,则可以运行fold2.mainfile模块,如果将pythonpath设置为下载代码>目录。 IE

> :: relative PYTHONPATH
> set PYTHONPATH=..
> python -m Fold2.MainFile
>
> :: or absolute PYTHONPATH
> set PYTHONPATH=c:\absolute\path\to\Downloads
> python -m Fold2.MainFile

fold2 \ mainfile.py文件。即,

> :: relative PYTHONPATH
> set PYTHONPATH=..
> python Fold2\MainFile.py
>
> :: or absolute PYTHONPATH
> set PYTHONPATH=c:\absolute\path\to\Downloads
> python Fold2\MainFile.py

如果您当前的工作目录是fold2,则可以运行mainfile模块,如果将pythonpath设置为下载>下载目录。 IE

> :: relative PYTHONPATH
> set PYTHONPATH=..\..
> python -m MainFile
>
> :: or absolute PYTHONPATH
> set PYTHONPATH=c:\absolute\path\to\Downloads
> python -m MainFile

mainfile.py文件。 IE

> :: relative PYTHONPATH
> set PYTHONPATH=..\..
> python MainFile.py
>
> :: or absolute PYTHONPATH
> set PYTHONPATH=c:\absolute\path\to\Downloads
> python MainFile.py

All you need is a proper combination of your current working directory, the PYTHONPATH environment variable, and the path to the script.

If your current working directory is Downloads, you can run the Demo.Fold2.MainFile module without modifying PYTHONPATH. I.e.

> python -m Demo.Fold2.MainFile

Or you can run the Demo\Fold2\MainFile.py file if you set PYTHONPATH to the current working directory. I.e.

> :: relative PYTHONPATH
> set PYTHONPATH=.
> python Demo\Fold2\MainFile.py
>
> :: or absolute PYTHONPATH
> set PYTHONPATH=c:\absolute\path\to\Downloads
> python Demo\Fold2\MainFile.py

If your current working directory is Demo, you can run the Fold2.MainFile module if you set PYTHONPATH to the Downloads directory. I.e.

> :: relative PYTHONPATH
> set PYTHONPATH=..
> python -m Fold2.MainFile
>
> :: or absolute PYTHONPATH
> set PYTHONPATH=c:\absolute\path\to\Downloads
> python -m Fold2.MainFile

or the Fold2\MainFile.py file. I.e.

> :: relative PYTHONPATH
> set PYTHONPATH=..
> python Fold2\MainFile.py
>
> :: or absolute PYTHONPATH
> set PYTHONPATH=c:\absolute\path\to\Downloads
> python Fold2\MainFile.py

If your current working directory is Fold2, you can run the MainFile module if you set PYTHONPATH to the Downloads directory. I.e.

> :: relative PYTHONPATH
> set PYTHONPATH=..\..
> python -m MainFile
>
> :: or absolute PYTHONPATH
> set PYTHONPATH=c:\absolute\path\to\Downloads
> python -m MainFile

or the MainFile.py file. I.e.

> :: relative PYTHONPATH
> set PYTHONPATH=..\..
> python MainFile.py
>
> :: or absolute PYTHONPATH
> set PYTHONPATH=c:\absolute\path\to\Downloads
> python MainFile.py
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文