英文:
ModuleNotFoundError: No module named 'XYZ', but when I prtint sys.path I can see the absolute path to folder I want to import
问题
我想从它们的所有文件和函数中添加它们,它们位于名为'tabele'的文件夹中,该文件夹位于与'gui'文件夹相同的目录中,'konverzijaZaGui.py'位于其中。
目录树视图
每次我尝试运行时都会显示:
ModuleNotFoundError: 找不到模块' tabele'
在我的代码中,我想从'tabele'中获取' citajIzTabele.py'
import sys
import os
temp = os.path.realpath("../tabele")
temp = os.path.abspath(temp)
sys.path.append(str(temp))
from tabele.citajIzTabele import *
当我打印sys.path时,我可以看到'tabele'的绝对路径,但它仍然说找不到模块
我尝试了相对路径添加模块,但每次都说找不到它
我的Python版本是3.10.9(64位)
英文:
I want to add all the files and functions from them, they are located in folder 'tabele' which is located in same directory as 'gui' folder where 'konverzijaZaGui.py' is located.
Tree view of directories
Every time when I try to run it says:
ModuleNotFoundError: No module named 'tabele'
In my code I want to get 'citajIzTabele.py' from 'tabele'
import sys
import os
temp = os.path.realpath("../tabele")
temp = os.path.abspath(temp)
sys.path.append(str(temp))
from tabele.citajIzTabele import *
When i print sys.path I can see the absolute path of 'tabele' but it still says no module found
I tried relative path to add module but every time it says it is not found
My version of python is 3.10.9 (64bit)
答案1
得分: 1
from tabele.citajIzTabele import *
将在sys.path的路径中查找tabele,但由于您已将tabele本身添加到sys.path中,它将在该路径内查找,但找不到它。因此,您可以将tabele的父目录添加到sys.path中,或者您可以执行以下操作:
from citajlzTabele import *
英文:
from tabele.citajIzTabele import *
Will look for tabele inside the paths of sys.path but since you added tabele itself to sys.path it will look inside that path and does not find it. So either you can add the parent directory of tabele to sys.path instead or you can do
from citajlzTabele import *
答案2
得分: 0
如果这是一个动态导入,那么我有答案。在页面顶部导入模块sys
和os
,然后输入以下代码:
path = os.path.dirname(__file__)
sys.path.append(path)
from folder import file
另外需要注意的是,如果你只想执行import folder
,那么你必须在其中有一个__init__.py
文件,因为这是它要导入的内容。对于你的问题,你可以使用变量path
进行一些字符串操作,以找到你要查找的文件夹。
英文:
If this is a dynamic import, then I have the answer. At the top of the page import
the modules sys
and os
and type the following code:
path = os.path.dirname(__file__)
sys.path.append(path)
from folder import file
Just an extra note, if you want to just do import folder
you have to have an __init__.py
file in there, as that is what it wants to import. With your problem, you can do some string manipulation with the variable path
to find the folder you're looking for.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论