python中如何从其他文件夹导入文件
我有一个名为 project 的文件夹,其中包含文件夹 dataprocessing 和文件夹 SVM。在 SVM 中的 classifier.py 文件中,我想从数据处理的两个文件中导入一个函数和一个数组。我在分类器文件中编写了以下内容:
from dataprocessing.sortdata import sorted_simulations
from dataprocessing.make_train_test import split
在 VScode 中,当我在文件中写入代码时,导入有效,但是当我运行该文件时,我收到 modulenotfound 错误,表示未找到“数据处理”。我尝试将 init.py 放入两个文件夹中,但仍然遇到同样的问题。有人知道我该如何解决它吗?
I have a folder called project that contains the folder dataprocessing and the folder SVM. In my classifier.py file, which is in SVM, I want to import a function and an array from two files from dataprocessing. I have written the following in my classifier file:
from dataprocessing.sortdata import sorted_simulations
from dataprocessing.make_train_test import split
In VScode when I write my code in the file, the import works, but when I run the file I get a modulenotfounderror, which says that "dataprocessing" is not found. I tried putting an init.py in both of the folders, but I still have the same problem. Do anybody have an idea of how I can solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就您而言,您当前的工作目录位于 classifier.py 所在的 SVM 中。然后,您尝试从数据处理文件夹和数据处理文件夹中的文件导入函数。在 svm 文件夹之外。
因此,在添加路径后尝试使用 sys 模块添加
路径,您可以编写如下
In your case, your current working directory is in SVM where your classifier.py exists. Then, you are trying to import functions from files that are in dataprocessing folder & outside the svm folder.
So try adding path using sys module
after adding path you can write as below