python没有模块从另一个文件夹导入.py时发现错误
我遵循a 非常接受的答案,这对我不起作用。 这是我所拥有的: 文件夹结构:
demo文件夹
fold1
img src =“ https://i.sstatic.net/ktire.png” alt =“在此处输入图像说明”>
另外,__ INIT __。py
是一个空文件。没有什么。
目标:import customfunction.py
来自demo
或fold1
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
Fold1
Fold2
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所需要的只是当前工作目录,
PythonPath
环境变量的正确组合以及脚本的路径。如果您当前的工作目录是
下载
,则可以运行demo.fold2.mainfile
模块而无需修改pythonpath
。 IE或您可以将
demo \ fold2 \ mainfile.py.py
文件运行,如果将pythonpath
设置为当前工作目录。即,如果您当前的工作目录是
demo
,则可以运行fold2.mainfile
模块,如果将pythonpath
设置为下载
代码>目录。 IE或
fold2 \ mainfile.py
文件。即,如果您当前的工作目录是
fold2
,则可以运行mainfile
模块,如果将pythonpath
设置为下载>下载
目录。 IE或
mainfile.py
文件。 IEAll 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 theDemo.Fold2.MainFile
module without modifyingPYTHONPATH
. I.e.Or you can run the
Demo\Fold2\MainFile.py
file if you setPYTHONPATH
to the current working directory. I.e.If your current working directory is
Demo
, you can run theFold2.MainFile
module if you setPYTHONPATH
to theDownloads
directory. I.e.or the
Fold2\MainFile.py
file. I.e.If your current working directory is
Fold2
, you can run theMainFile
module if you setPYTHONPATH
to theDownloads
directory. I.e.or the
MainFile.py
file. I.e.