Python3.7.2 module import issu

the code in the following file structure
clipboard.png
_ _ init__.py:

c = 12

Code in ff.py:

from . import c
print(c)

run ff.py to report an error directly:

Traceback (most recent call last):
  File "ff.py", line 1, in <module>
    from . import c
ImportError: cannot import name "c" from "__main__" (ff.py)

use absolute path"." Referring to folder1, it should be introduced to report errors for peace and society. Python3.7.2 .

May.17,2022

first, each module has a _ _ name__ attribute that identifies the name of the module. If a module is imported through import, its name will be the package name. The module name, such as import a.b.c, then c.roomname _ will be 'a.b.c'. And if you are from b import c in the a directory, then c.roomname _ will be'b. C'. If a module is executed directly with the python interpreter, it will be considered top-level module, and its _ _ name__ will be _ _ main__.

when using relative imports, python looks for the import path by tracing up the corresponding level from the module's _ _ name__, and then trying to import the specified module below that level. Again, in the above example, if the current c. Roomnames _ is a. B. C module, use from in the c module. Import DJ python will look for module d under package a.b, which is equivalent to import a.b.d. Use from in the c module.. Import eMagol python will look for module e under package a, which is equivalent to import a.e.

and what happens if you execute python c.py directly under the directory Agamb? At this point c becomes top-level module, and its _ _ name__ will be'_ _ main__', so when python tries to parse the from in the c module. In the import d statement, it is found that the _ _ name__ of the current module is _ _ main__, cannot be traced up one level, so an error is reported: ImportError: cannot import name 'd'from'_ _ main__' (in Python2.x, this error will be ValueError: Attempted relative import in non-package). So we come to the conclusion that cannot use relative imports in the main module executed directly by the interpreter.

back to the subject's question, then I want to use from in ff.py. What does import c do? According to the above conclusion, we must not execute ff.py directly at this time, but import it as a module by using the -m option of python. We need to execute python-m folder1.ff in the trymodule folder (note that the module should be written as folder1.ff instead of the file path as folder1/ff.py), so the _ _ name__ of the module ff becomes folder1.ff, and at this point from. Import c will be parsed to import folder1.c, and our goal will be achieved.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e9aa35-17f0.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e9aa35-17f0.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?