在同一目录中导入模块不起作用

发布于 2025-01-21 00:20:02 字数 1107 浏览 2 评论 0原文

我有一个带有两个模块 loadfromdata writeindata 的dir。第一个叫第二个。我在开始时导入第二个,甚至自动建议都列出了我要调用的功能。一旦我运行代码,我就会得到 modulenotfoundError

directory

模块1

import WriteInData as wd
[...]
def createNewFile(filePath: str) -> bool:
    #create pw file if not existent
    try:
        if wd.writeInitalData(filePath):
            print("The directory path {} was created".format(filePath))
            return True
    except FileExistsError:
        pass #writeLog
    else:
        return False

Module2:

def writeInitalData(filePath: str) -> bool:
    with open(filePath, 'w') as csvfile:
        filewriter = csv.writer(csvfile, delimiter=',',quotechar='|', quoting=csv.QUOTE_MINIMAL)
        filewriter.writerow(['id','name','description','password','created_on','last_modified_on'])
    return True

到目前为止我尝试的内容:

  • 重命名模块
  • 调用来自模块
  • 调用另一个模块的

另一个函数。看来我的进口方式是错误的,但我不知道我到底在做什么错

I have a dir with the two modules LoadFromData and WriteInData. The first one calls the second one. I import the second one at the beginning and even the auto suggestion lists the function I want to call. As soon as I run the code I get an ModuleNotFoundError

Directory

Module 1

import WriteInData as wd
[...]
def createNewFile(filePath: str) -> bool:
    #create pw file if not existent
    try:
        if wd.writeInitalData(filePath):
            print("The directory path {} was created".format(filePath))
            return True
    except FileExistsError:
        pass #writeLog
    else:
        return False

Module2:

def writeInitalData(filePath: str) -> bool:
    with open(filePath, 'w') as csvfile:
        filewriter = csv.writer(csvfile, delimiter=',',quotechar='|', quoting=csv.QUOTE_MINIMAL)
        filewriter.writerow(['id','name','description','password','created_on','last_modified_on'])
    return True

What I tried so far:

  • rename the module
  • call another function from the module
  • call another module

None of this works. It seems the way I import is wrong, but I don't know what exactly I'm doing wrong

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

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

发布评论

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

评论(1

嗫嚅 2025-01-28 00:20:02

我问题的解决方案是添加从SRC开始的完整目录路径,尽管它是相同的目录。
因此:

  • 错误:导入writeindata
  • 对:导入src.main.controller.writeindata

The solution of my problem was to add the full directory path starting from src, although it is the same directory.
Therefore:

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