Python import folder
I'm using Python 2.6. I'm new at Python programming, so forgive my ignorance. I have multiple directories with multiple packages. My directory structure looks like this:
/disconfig
__init__.py
/LLE
__init__.py
/DIS
__init__.py
/Data
__init__.py
/common
__init__.py
/LLE
__init__.py
I need to be able to import both of the LLE directories to make the program work. I can't add disconfig or common to PYTHONPATH because they both have an LLE directory. When I do import LLE, it only pulls in one of them. I've tried doing:
import disconfig.LLE
it errors saying:
Traceback (most recent call last):
File "./disconfig.py", line 9, in <module>
import disconfig.LLE
File "./disconfig.py", line 9, in <module>
import disconfig.LLE
ImportError: No module named LLE
I've tried:
import disconfig
This works, but when I try to run code from within one of the modules:
LLE.DIS.runDisFunc
and it comes back saying
name 'LLE' not defined
If I try:
disconfig.LLE.DIS.runDisFunc
it says errors with:
'module' object has no attribute 'LLE'
I've been working on this for so long and can't wrap my brain around it. Any suggestions?
EDIT: Maybe there is one more thing to mention. The files that are in these directories are generated by slice2py from ZeroC. They place all of the generated .py files in the top-level directory (so under /disconfig). The LLE directory has the init.py that has the imports of all of the generated .py files as well as "import DIS" and "import Data". Then in DIS and Data, there are init.py files that include the imports specific to 开发者_如何学编程those modules. So, more completely it looks like:
/disconfig
__init__.py
Attribute_ice.py
DIS_ice.py
DISControl_ice.py
/LLE
__init__.py
/DIS
__init__.py
/Data
__init__.py
If I change the module from disconfig to MDIS (as suggested) and do
import MDIS
I get
Traceback (most recent call last):
File "./disconfig", line 9, in <module>
import MDIS
File "/oudvmt/python/MDIS/__init__.py", line 18, in <module>
import LLE
File "/oudvmt/python/MDIS/LLE/__init__.py", line 4, in <module>
import Attribute_ice
ImportError: No module named Attribute_ice
If I try
import MDIS.LLE
I get
Traceback (most recent call last):
File "./disconfig", line 9, in <module>
import MDIS.LLE
File "/oudvmt/python/MDIS/__init__.py", line 18, in <module>
import LLE
File "/oudvmt/python/MDIS/LLE/__init__.py", line 4, in <module>
import Attribute_ice
ImportError: No module named Attribute_ice
I've tried moving the generated .py files into the subdirectories, but that caused other problems because the files in /DIS depend on the files in /Data (DIS_ice.py imports Attribute_ice.py, which is part of LLE/Data). If I separate them, I get ImportErrors.
More EDIT: I added all of the .py files to my init.py in the /MDIS directory and removed them from the init.py in the subdirectories. Now I have nore more import errors using just "import MDIS". However, now when I try my function
disadmin = MDIS.LLE.DIS.DISAdminPrx.checkedCast()
I get
'module' object has no attribute 'DISAdminPrx'
In DISAdmin_ice.py, there is a class called DISAdminPrx and it does have a method of checkedCast. I tried
disadmin = DISAdmin_ice.DISAdminPrx.checkedCast()
and
disadmin = MDIS.LLE.DIS.DISAdmin_ice.DISAdminPrx.checkedCast()
and any other combination I could think of.
EDIT AGAIN Looks like it's a problem with the python converter I'm using from ZeroC. They are helping me resolve it. Thanks for the help!
It appears that your script is named disconfig.py
so when you import disconfig
you call the script. You should named your script differently from your module.
EDIT
before
disadmin = MDIS.LLE.DIS.DISAdmin_ice.DISAdminPrx.checkedCast()
you should do
import MDIS.LLE.DIS.DISAdmin_ice
or do
from MDIS.LLE.DIS.DISAdmin_ice import DISAdminPrx
DISAdminPrx.checkedCast()
I really think that what I propose work but I can't explain why if someone could explain why in comprehensive words I'm sure the OP would be glad. It is explained in http://docs.python.org/tutorial/modules.html#packages but I'm not sure that is in simple words.
You need an import LLE
statement in disconfig/__init__.py
, then import disconfig.LLE
should work.
This will work:
import disconfig.LLE.DIS
disconfig.LLE.DIS.runDisFunc()
This is because Python does not automatically imports subpackages.
write your own import file then import that this was made to import my scripts. repeat for every folder
#scripts
os.system("dir /b Scripts\\*.py > dirb.txt")
file1=open("dirb.txt","r")
file2=open("temp.py","w")
for lines in file1.readlines():
lines=lines.strip()
string="import Scripts."+str(lines[0:-3])
file2.write(string+"\n")
file1.close()
file2.close()
from temp import *
os.system("rem temp.txt")
精彩评论