473,653 Members | 3,000 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Some Class
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.Some Class
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 1410
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.Some Class
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.Some Class
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.Some Class" 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.test 2.someclass import SomeClass
print SomeClass

or

from importtest.test 2 import someclass
print someclass.SomeC lass

--
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
2744
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, a module change its interface or its implementation in a fundamental way (an example: wxPython). This, I think, can be resolved by allowing an user to explicitly say what version of a module it wants (sush as version numbers in Linux shared...
3
3810
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 'import os\ndef test():\n print os.getcwd()' > os.py cd /tmp
5
2468
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 kk in k:
2
22706
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 format. I'm trying to figure out how to import that into SQL Server 2000 so that I'll be able to manipulate it in a gui format, rather than command line. I can't find any import that takes a .sql file. I've been trying to load it into the query...
1
8174
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 fields. 177 102003 16:43:12 102003 18:43:12 6OAG0ADP Y 0000 0000 0000 0000 61930 4HGA800 165 102003 17:43:12 102003 18:44:12 6OAG0ADP Y 0000 0000 0000 0000 61930 4HGA800 177 102003 16:41:18 102003 18:45:12 6OAG0ADP Y 0000 0000 0000 0000 61930...
4
3016
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 coming to a firm conclusion over whether it was the better approach, and wonder if I should do it differently the next time I'm faced with a similar issue. I needed an app to automatically import from spreadsheets with a semi-dynamic structure,...
3
3706
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 - and dump everything into a table. The problem is once I have the data imported into a new table, I can't do much with it. If I try to run an Append query and insert data from the new table into an existing table, the query fails - "Error...
6
2357
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 problem also with other imports (e.g. urllib2 etc.). And again very often in all these cases where I get weired Python exceptions, the problem is around re-functions - usually during re.compile calls during import (see some of the exceptions below). But...
11
1485
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
3912
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 isomorphism between filesystem and namespace, doesn't seem very well suited for big projects. However, I might not really understand the Pythonic way. I'm not sure if I have a specific question here, just a general plea for advice. 1) Namespace....
0
8370
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8811
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8704
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5620
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4147
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2707
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.