473,395 Members | 1,556 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Import Problems

Once again, I am having issues with imports...

Until now, I thought the general guidelines were to rarely use 'from x
import y' syntax, except when you really want to copy names over.
However, I have run into issues by following this guideline. So...

1) What is going wrong in the example below?
2) What is a good way to handle imports for packages w/subdirectories?

Here is a sample directory structure:

importtest/
__init__.py
test2/
__init__.py
someclass.py
mytest.py
Here are the definitions of the files:

# importtest/__init__.py
from test2 import *

# importtest/test2/__init__.py
from someclass import *
from test2 import *

# importtest/test2/someclass.py
class SomeClass:
pass

# importtest/test2/mytest.py
import importtest
print importtest.SomeClass
On import I get the following:
>>import importtest
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/home/user/lib/python/importtest/__init__.py", line 1, in ?
from test2 import *
File "/home/user/lib/python/importtest/test2/__init__.py", line 2, in ?
from mytest import *
File "/home/user/lib/python/importtest/test2/mytest.py", line 3, in ?
print importtest.SomeClass
AttributeError: 'module' object has no attribute 'SomeClass'
>>>

The problem seems to be an 'order' issue. importtest/test2/__init__.py
has loaded someclass.py, but it doesn't seem to have copied its contents
into importtest/__init__.py....perhaps it is because it hasn't finished
all of its imports. Is this correct?

So what is a good way to deal with this? In files which contain
implementations, I thought it was best not to use 'from x import y', but
this seems to be the only way to get this to work:

# importtest/test2/mytest.py
from someclass import SomeClass
print SomeClass

Is this the guideline?
Use 'from x import y' for modules within your package.
Use 'import y' for modules outside your package.

Help.
Apr 27 '07 #1
2 1395
Bill Jackson wrote the following on 04/27/2007 12:49 PM:
# importtest/test2/__init__.py
from someclass import *
from test2 import *
Sorry typo here:

# importtest/test2/__init__.py
from someclass import *
from mytest import *
Apr 27 '07 #2
Bill Jackson wrote:
Once again, I am having issues with imports...

Until now, I thought the general guidelines were to rarely use 'from x
import y' syntax, except when you really want to copy names over.
No, the guideline is to not use "from x import *" except at the interactive
prompt and occasionally in __init__.py's to "copy names over" as you put it.
However, I have run into issues by following this guideline. So...

1) What is going wrong in the example below?
2) What is a good way to handle imports for packages w/subdirectories?

Here is a sample directory structure:

importtest/
__init__.py
test2/
__init__.py
someclass.py
mytest.py
Here are the definitions of the files:

# importtest/__init__.py
from test2 import *

# importtest/test2/__init__.py
from someclass import *
from test2 import *

# importtest/test2/someclass.py
class SomeClass:
pass

# importtest/test2/mytest.py
import importtest
print importtest.SomeClass
On import I get the following:
>>import importtest
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/home/user/lib/python/importtest/__init__.py", line 1, in ?
from test2 import *
File "/home/user/lib/python/importtest/test2/__init__.py", line 2, in ?
from mytest import *
File "/home/user/lib/python/importtest/test2/mytest.py", line 3, in ?
print importtest.SomeClass
AttributeError: 'module' object has no attribute 'SomeClass'
>>>


The problem seems to be an 'order' issue. importtest/test2/__init__.py
has loaded someclass.py, but it doesn't seem to have copied its contents
into importtest/__init__.py....perhaps it is because it hasn't finished
all of its imports. Is this correct?
You are right that it is an order issue. The line "from test2 import *" hasn't
finished executing by the time "print importtest.SomeClass" is executed because
the former is trying to execute the latter.
So what is a good way to deal with this? In files which contain
implementations, I thought it was best not to use 'from x import y', but
this seems to be the only way to get this to work:
My recommendation is that inside packages, be as specific as is feasible with
your imports, even if you expose the symbols at a higher level for external
callers. If you have a module whose contents are being exposed in your
__init__.py, never rely on the contents of that __init__.py in that code.
Instead, go straight to the real modules that implement the symbols that you
need. So mytest.py should read:

from importtest.test2.someclass import SomeClass
print SomeClass

or

from importtest.test2 import someclass
print someclass.SomeClass

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Apr 27 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Manlio Perillo | last post by:
Hi. I'm a new user of Python but I have noted a little problem. Python is a very good language but it is evolving, in particular its library is evolving. This can be a problem when, ad example,...
3
by: George P | last post by:
I've run into a strange package related namespace problem. Follow these steps (on UNIX system) to illustrate: ------------------------- mkdir /tmp/mypkg cd /tmp/mypkg touch __init__.py echo...
5
by: Steve Holden | last post by:
This is even stranger: it makes it if I import the module a second time: import dbimp as dbimp import sys if __name__ == "__main__": dbimp.install() #k = sys.modules.keys() #k.sort() #for...
2
by: jet | last post by:
Hi, Maybe this is an easy task, but I'm having a really hard time figuring out how to do this. I'm a complete newbie to SQL Server. I have a database dump file from MySQL that's in .sql...
1
by: mark | last post by:
In Access 2000 and 2002, I have created an import specification to import the fixed-width recordset below into an existing table. I am having strange problems with the import of the date and time...
4
by: Steve Jorgensen | last post by:
I'm restarting this thread with a different focus. The project I'm working on now id coming along and will be made to work, and it's too late to start over with a new strategy. Still, I'm not...
3
by: deko | last post by:
I've been trying to use the Access Import Wizard to expedite importing data into my mdb. The nice thing about the wizard is that I can import from different file formats - txt, xls, even Outlook -...
6
by: robert | last post by:
I get python crashes and (in better cases) strange Python exceptions when (in most cases) importing and using cookielib lazy on demand in a thread. It is mainly with cookielib, but remember the...
11
by: Jim | last post by:
Hi, I have created an import module. And would like to access a function from the main script, e.g., file abc.py: ################### def a(): m() return None
49
by: Martin Unsal | last post by:
I'm using Python for what is becoming a sizeable project and I'm already running into problems organizing code and importing packages. I feel like the Python package system, in particular the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.