Archived

This topic is now archived and is closed to further replies.

Odoacer

noobish python question

Recommended Posts

Okay, I've been hacking Python for a while and it's been fun so far. One thing I can't figure out is how to import locally created files? (ie, a class in another .py file). Sorry if it sounds dumb but I can't find any concrete info anywhere in terms of Windows - I see things like "setenv" and stuff but that seems to be geared towards Unix. Is there a simple way to do it as in C (ie, replacing < file> with "file" ?) edit:formatting [edited by - Odoacer on April 22, 2004 11:26:34 PM]

Share this post


Link to post
Share on other sites
http://www.python.org/doc/2.3.3/tut/node8.html#SECTION008400000000000000000

Though, the short version is this:
Say you write a class named 'SomeClass' in a file called 'somefile.py' ... then to access it from 'anotherfile.py' in the same directory you would write (in anotherfile.py)

from somefile import SomeClass

Then you can then instantiate it etc as if it the class were defined in somefile.py

- Jacob

[edited by - Kevlar-X on April 22, 2004 11:39:49 PM]

Share this post


Link to post
Share on other sites
Ohh yeah. I forgot you had to prefix the module name to use an external function. D'oh! Still getting used to that aspect of Python...

Thanks for the quick reply.

edit: I don't have this problem, but I was just wondering: what if your external file is in a different directory? How would you import it then?

[edited by - Odoacer on April 22, 2004 11:46:48 PM]

Share this post


Link to post
Share on other sites
Import * is bad form, and should be avoided unless absolutely needed.

Also, its not really the only way to avoid using the module name, what I mentioned above also does that.

Share this post


Link to post
Share on other sites