开发者

how to import a.py not a folder

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

in a.py is :

c='ccc'

in b.py is :

import a

print dir(a)

when i execute b.py ,it show (it import 'a' folder):

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

and when i delete a folder, it show ,(it import a.py):

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

so my question is :

how to import a.py via not delete a folder

thanks

updated

i use imp.load_source, so in b.py 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

it is ok now ,and print 'ccc'

and

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

thanks

updated2

it is ok now :

imp.load_source('anyname',path)

from anyname import *
print c
开发者_运维技巧

it show 'ccc'

updated3

it is also ok:

import imp,os
imp.load_source('anyname','aaa.py')
from anyname import *
print c


Use imp.load_module - there you can specify the file directory, overriding the behaviour of import.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜