Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

python - How to import a file that is in a top level folder?

Is it possible for me to import the file "imagensAlertLoginExist.py" into the file "alertLogin.py"? If possible how do I import?

I had never experienced this problem, so I always made my imports like this:

from folder1.folder2 import file

or:

from folder1.folder2.file import class

or like this:

from folder1.folder2.file import method

However, the file I want to import (imagesAlertLoginExist.py) is not in the same folder or level below my main code (alertLogin.py), therefore, this file (imagesAlertLoginExist.py) is not visible to my main code. How do I, when importing, specify that the module I want to import is in a folder above?

Taking the minimum example into consideration, it is as if the module I want to import was in folder0.

file structure:

main.py
templates
    images
        alertLoginExist
            imagensAlertLoginExist.py
    interfaces
        alertLogin.py

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If your files structure is like this then you need to import like this for example:-

enter image description here

If you want to import a "class" from file imagensAlertLoginExist.py into alterLogin.py then you can import that class like this:-

File:-alterLogin.py

from template.images.alterLoginExist.imagensAlertLoginExist import class_name
             ***Please let me know if it not works.***

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...