473,473 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Strange import behavior

Hey guys,

I'm having problems importing a file into my python app. Everytime I
try to define the object specified by this file, i.e,

test = Test(),

It raises an ImportError exception: ImportError: cannot import name
Test.

I've declared it as:

from test import Test (and I've also tried from test import *)

As luck would have it, if I try creating a newfile (say test2) and then
declare the same class as:

from test2 import Test()

It works fine! I tried doing this with the first couple of files, but
then another error crops up for another file giving me this
ImportError...so I'd end up having to re-copy a bunch of files over
again.

I feel like there may be some sort of dependency screw up or
whatever..Is there a command that's like "Python, chill, let's start
from scratch and build this thing"?

Sep 1 '06 #1
2 1704
unexpected wrote:
I'm having problems importing a file into my python app. Everytime I
try to define the object specified by this file, i.e,

test = Test(),

It raises an ImportError exception: ImportError: cannot import name
Test.

I've declared it as:

from test import Test (and I've also tried from test import *)

As luck would have it, if I try creating a newfile (say test2) and then
declare the same class as:

from test2 import Test()

It works fine!
your code samples make very little sense, and the last one isn't even
valid Python syntax.

maybe you could post a complete self-contained example (including the
file names, and what's in what file), instead of typing from memory ?

</F>

Sep 1 '06 #2
unexpected wrote:
Hey guys,

I'm having problems importing a file into my python app. Everytime I
try to define the object specified by this file, i.e,

test = Test(),

It raises an ImportError exception: ImportError: cannot import name
Test.

I've declared it as:

from test import Test (and I've also tried from test import *)

As luck would have it, if I try creating a newfile (say test2) and then
declare the same class as:

from test2 import Test()

It works fine! I tried doing this with the first couple of files, but
then another error crops up for another file giving me this
ImportError...so I'd end up having to re-copy a bunch of files over
again.

I feel like there may be some sort of dependency screw up or
whatever..Is there a command that's like "Python, chill, let's start
from scratch and build this thing"?
Replace

from test import Test

with

import test
print test.__file__
raise SystemExit

The test file you are importing is probably not the one you expect. To fix
that make sure that the directory of /your/ test.py is in sys.path /before/
the directory of the test file you are accidentally importing. Even better,
rename it to something unambiguous as the name 'test' is already taken by
the test package of a vanilla Python installation. For example, I get
>>import test
test.__file__
'/usr/local/lib/python2.4/test/__init__.pyc'

The ImportError is actually an AttributeError in disguise:
>>import test
test.Test
Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: 'module' object has no attribute 'Test'
>>from test import Test
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: cannot import name Test

Peter

Sep 1 '06 #3

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

Similar topics

1
by: Dan Stromberg | last post by:
The below small program is giving strange behavior. At the bottom of the code, please find "this works" and "this doesn't work" comments. Why does one work and the other not? TIA. ...
0
by: John Hunter | last post by:
I have a class that uses some extension code I have written and I am trying to track down some memory leaks, which I presume to be in my extension code. The class is question is in a python...
3
by: Christoph Zwerschke | last post by:
Just hitting a strange problem with Python import behavior. It is the same on all Python 2.x versions and it is probably correct, but I currently don't understand why this happens. I have...
0
by: Michael J. Bigos | last post by:
Has anyone else come across this? We are building an ASP.Net application that uses a certficate in the local machine store to sign XML data before transmitting it to a third-party. The third...
0
by: hagaiy | last post by:
Has anyone else come across this? We are building an ASP.Net application that uses a certificate and private key in the local machine store. (we are testing it both on windows 2000 & 2003) We...
0
by: Piotr Kosinski | last post by:
Hi, I noticed strange behaviour in Excel. I wrote small shared add in for Excel. This util create button in standard bar. Button click provides to open a form which has to import data from sql...
5
by: IloChab | last post by:
Sorry I wasn't able to be more specific on my topic but I really do not know how to classify my problem, I mean that I can't understand if it's a python or a twisted or a Qt4 problem I'm...
3
by: sara | last post by:
Very strange behavior, but I suspect some is A2K and some might be for me to correct. Just trying to see if anyone can help and advise. We have a database that's been running for a few years...
4
by: Chris | last post by:
Hi, I'm puzzled by some strange behavior when my Python/Tkinter application quits (on linux): the terminal from which I started Python is messed up. If start up python, then import the code...
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
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
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...
1
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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...
0
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...
0
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 ...

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.