how to get the path of a module (myself) ?

Steven D'Aprano steve at
Tue Jun 2 03:51:20 CEST 2009

On Tue, 02 Jun 2009 02:55:13 +0200, Stef Mientki wrote:

>> What are you trying to do? Using execfile is probably not the right
>> solution.
> Maybe you're right, and it's not the best solution for my problem. I've
> written a program, that contains many files, both python files and data
> files,
> and I would like to distribute the program. For Linux, I'll just bundle
> the files in a zip-file, but for windows I want to make a one button
> installer, and the files generated by Py2Exe, don't work at all.

Define "don't work at all".

> Through this discussion, I discovered another problem, because __file__
> isn't the current file, I can't run 1 module(file) from another module
> (file) .

It sounds like you are trying to fight Python's module system instead of 
working with it. In Python programs, you don't "run" other modules, you 
import them, then call functions inside the module. You treat the modules 
you write exactly the same as standard library modules.

As a general rule, you would never say:


Instead you would say:

import os

The same rule applies for your modules. As a general rule, NEVER say:


instead do:

import mymodule

(There are exceptions, but if you need to ask what they are, you're not 
ready to learn them! *wink*)

> The structure of my files is something like this:
> Base_Path
>   Main_Program_Path
>     Brick_Path
>   Support_Libraries
>   Sounds_Path
>     Sound_1.wav
>     Sound_2.wav
>   Picture_Path
>     Picture_1.png
>     Picture_2.bmp

Are these packages? Or just random folders with random files in them?

> The Main_Programs, should be able to "run/launch" other Main_Programs
> and Support_Libraries,
> in several ways (wait / nowait, fetch output or not, ... ). So each
> Support_Libraries will have a "main-section". 

Sounds like a horribly fragile, complicated and complex way of doing 

> Everything is highly
> dynamical, just dumping a new py-file in the Brick_Path, will make the
> py-files available ( i.e. directly visible and usable to the user) in
> all Main_Programs.

Sounds like you're trying to implement plugins. For that, __import__() is 
your friend. execfile() is *not* your friend -- you might think it is, 
but don't listen to it. It's like one of those guys who calls you "buddy" 
and talks you into doing things that you regret the next day. Trust me, 
use execfile() and you'll end up waking up naked in an alleyway in Vegas 
married to someone called Lula-Mae, with no memory of the last three days 
and a tattoo of Paris Hilton on your butt.

> Distributing the files through Py2Exe doesn't work at all. So I was
> thinking of a hack:

As soon as you start thinking of using a hack in production code, go take 
a cold shower and lie down until the urge goes away.

> Any suggestions ?

Have you tried to find out why Py2Exe is broken and fix that?


More information about the Python-list mailing list