1

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
Iniciante
  • 23
  • 4

2 Answers2

0

As per Python docs:

When specifying what module to import you do not have to specify the absolute name of the module. When a module or package is contained within another package it is possible to make a relative import within the same top package without having to mention the package name. By using leading dots in the specified module or package after from you can specify how high to traverse up the current package hierarchy without specifying exact names. One leading dot means the current package where the module making the import exists. Two dots means up one package level. Three dots is up two levels, etc. So if you execute from . import mod from a module in the pkg package then you will end up importing pkg.mod. If you execute from ..subpkg2 import mod from within pkg.subpkg1 you will import pkg.subpkg2.mod

In your case you can do it like this (If you are executing main.py file):
 main.py

from template.interfaces import alertLogin  

 alertLogin.py

print("Imported alertLogin")
from template.images import imagensAlertLoginExist

 imagensAlertLoginExist.py

print("Import imagensAlertLoginExist")
  • Hi, thanks for the reply. I tried: ` from ..images.alertLoginExist import imagensAlertLoginExist` and returned the following error: `from ..images.alertLoginExist import imagensAlertLoginExist` `ImportError: attempted relative import with no known parent package -` – Iniciante Jan 09 '21 at 19:22
  • Can you please share in which code file are you importing imagensAlertLoginExist.py and which file are you executing? (I suppose that can be main.py) – 21 PROmode Jan 10 '21 at 09:40
0

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.***
  • Unfortunately it didn't work. Returned the error 'ModuleNotFoundError: No module named' template '', because the search for the module starts from which the entry point script is being executed as described [here](https://stackoverflow.com/questions/4383571/importing-files-from-different-folder/4383597#comment53217573_4383597)!. This would be resolved if I added "imagesAlertLoginExist.py" to sys.path, however this solution is not viable for my problem. – Iniciante Jan 09 '21 at 20:51
  • I don't want to import a class from the "imagesAlertLoginExist.py" file, I want to import the file itself, as it is a Qt resource file for Python. – Iniciante Jan 09 '21 at 20:59
  • Actually you got this error because your folder name is "templates" not "template" in you app:- error 'ModuleNotFoundError: No module named' template '', if you want to import "imagesAlertLoginExist.py" file so please try this it works for me, hope it works for you also :- `from templates.images.alterLoginExist import imagensAlertLoginExist` you can check in this also:-https://stackoverflow.com/questions/31643756/error-import-views-in-urls-py – Rohit Bhati Jan 10 '21 at 06:47
  • Unfortunately the error still continues. Can you develop with the 21 PROmode example? (Returning pasta with '..') – Iniciante Jan 11 '21 at 03:23