[Newbie:]Importing modules from different files

Jesper Hertel jh at cddk.dk
Thu Sep 9 10:25:20 EDT 1999


You have to put a file called "__init__.py" into the new directory. This
makes the directory a Python package.

Let's say the name of the new directory is "spam". When you use "import
spam", you import the contents of the __init__.py file as a module. If you
put a file, e.g. "mymodule.py", inside the spam directory, you can import
this module using "import spam.mymodule" or "from spam import mymodule" -
but again, this only works if you created the (possibly empty) file
"__init__.py" in the "spam" directory.

Hope this helps.

Jesper Hertel

<gtnorton at my-deja.com> wrote in message news:7r8dl0$cao$1 at nnrp1.deja.com...
>                            Hello folks,
>                                       Up until now, I've been putting
> all of my modules in my main Python folder.But now it grows too large to
> find quickly.When I create a folder(still in the main Python file) for
> the scripts and try to import them with: >>>import spam    I get back
> "no module named spam".Please clue me in.
>                                 Thanks in advance
>                                               gtnorton
> (P.S.)Running Windows98
>
>
> Sent via Deja.com http://www.deja.com/
> Share what you know. Learn what you don't.






More information about the Python-list mailing list