How to import a.py not a folder

zjm_code |-----a.py |-----a |----- __init__.py |-----b.py 

in a.py:

 c='ccc' 

in b.py:

 import a print dir(a) 

when I execute b.py it shows (it imports the folder 'a'):

 ['__builtins__', '__doc__', '__file__', '__name__', '__path__'] 

and when I delete the folder, it will show (imports a.py):

 ['__builtins__', '__doc__', '__file__', '__name__', 'c'] 

so my question is:

how to import a.py through do not delete the folder

thanks

updated

I am using imp.load_source, so in b.py there is:

 import imp,os path = os.path.join(os.path.dirname(__file__), os.path.join('aaa.py')) ok=imp.load_source('*',path) print ok.c 

now it's ok and type 'ccc'

and

how to show 'ccc' via "print c" not via "print ok.c" ???

thanks

updated2

now this is normal:

 imp.load_source('anyname',path) from anyname import * print c 

he shows 'ccc'

updated3

this is also normal:

 import imp,os imp.load_source('anyname','aaa.py') from anyname import * print c 
+4
source share
2 answers

Use imp.load_module - there you can specify the file directory by overriding the import behavior.

+2
source

Rename the folder to a different name. A folder with the same name takes precedence.

+1
source

Source: https://habr.com/ru/post/1309748/


All Articles