Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Import Problems, Attribute Error 1

Status
Not open for further replies.

cpusorcerer

Programmer
Jul 6, 2006
3
US
Hi, I'm new to Python and I have an import problem... I can import the folder that this .py file is in, but I can't seem to access the .py file itself, either by importing or just using ContainingFolder.pyFile.desiredFunction(). I've looked through my Python documentation, Python books(Learning and Programming) and I've looked online, but no luck so far. So I was wondering if you guys could help. Can you think of any reason why trying to access a function in this .py file would raise the attribute error: no such attribute(when it's spelled correctly with the correct path through its containing file- pyISBNdb.abstract.Response(...))? pyISBNdb is the containing file, abstract is the .py file I can't seem to import or reference with dot qualifications. Thanks for your consideratrion.

-Matt
 
Have you imported the .py file?

If the file is called File.py, you need the following line to use it:

import File

at the head of the file using it. Also, to access functions, you use:

File.FUNCTIONNAME

Hope it helps.
 
Yes, Ive tried importing the file like that. I've also tried using the dot qualification File.FUNCTIONNAME. It hasn't worked. Thanks for the attempt to solve it, though. Can you think of any other reasons why my program wouldn't work?

-Matt
 
I can import the folder that this .py file is in

seems to me that you have imported a package (folder with __init__.py)

but I can't seem to access the .py file itself, either by importing or just using ContainingFolder.pyFile.desiredFunction().

is your folder really named with a .py extension?

to use a module (.py file) inside a package,
use any of the following

Code:
import ContainingFolder
ContainingFolder.pyFile.desiredFunction()

or

Code:
from ContainingFolder import pyFile
pyFile.desiredFunction()
 
Hey guys, thanks for the help. I actually found out that I wasn't doing anything wrong except for using JYTHON. :~( It turns out it was silently failing in its importing because one of the files imported by the package's init.py file was a file that Jython didn't like(it was incompatible... generated a syntax error, of all things- and we're talking about popular python libraries. This error was in urllib2.). I ran the same file with regular (C-based) Python and it worked like a charm. I wouldn't have known what was wrong if I hadn't guessed at the problem being with Jython, as my python books said my code was correct. It was painless to test the code with regular python and that's how I found the bug.
Sooooo... I guess unless you're willing to rewrite a few lines- or maybe vast chunks, for all I know- of fairly standard python libraries(it WAS in /usr/lib/Python2.4), you might want to be wary of Jython. Thanks again for all the help and good luck in your coding endeavors.

-Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top