在同一目录中导入模块不起作用
我有一个带有两个模块 loadfromdata 和 writeindata 的dir。第一个叫第二个。我在开始时导入第二个,甚至自动建议都列出了我要调用的功能。一旦我运行代码,我就会得到 modulenotfoundError
模块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
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我问题的解决方案是添加从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:
import WriteInData
import src.main.Controller.WriteInData