Module organization

  • Thread starter =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=
  • Start date
?

=?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=

I am slowly learning Python and I'm already starting to write some minor
modules for myself. Undoubtedly there are better modules available
either built-in or 3rd party that do the same as mine and much more but
I need to learn it one way or another anyway.

What I'm wondering about is module organization.

I created my own directory for storing my modules and added the full
path to this to PYTHONPATH (Windows XP platform).

This means that "import modulename" works for my modules now.

However, the name of my module is "db" or "database", a module name that
will likely conflict with other modules I might encounter later.

As such, I was thinking of doing the same that the "distutils" set of
modules have done, by creating a subdirectory and storing them there, so
I created a "lvk" directory in that directory of mine and moved the
module in there, but now "import lvk.modulename" doesn't find the module.

Is there a trick to this? Do I have to store my own modules beneath
C:\Python24\Lib? or can I use the organization I've tried just with some
minor fixes to make python locate my modules?
 
R

Robert Wierschke

Lasse said:
I am slowly learning Python and I'm already starting to write some minor
modules for myself. Undoubtedly there are better modules available
either built-in or 3rd party that do the same as mine and much more but
I need to learn it one way or another anyway.

What I'm wondering about is module organization.

I created my own directory for storing my modules and added the full
path to this to PYTHONPATH (Windows XP platform).

This means that "import modulename" works for my modules now.

However, the name of my module is "db" or "database", a module name that
will likely conflict with other modules I might encounter later.

As such, I was thinking of doing the same that the "distutils" set of
modules have done, by creating a subdirectory and storing them there, so
I created a "lvk" directory in that directory of mine and moved the
module in there, but now "import lvk.modulename" doesn't find the module.

Is there a trick to this? Do I have to store my own modules beneath
C:\Python24\Lib? or can I use the organization I've tried just with some
minor fixes to make python locate my modules?

I'm also new to python. But I think the solution to your problem is:

make the directory - lvk - a python package

in order to do this you just add an file __ini__.py (two underscores
befor and after init)in the directory.
__init__.py can be empty or include some initialization for your package
or a docstring for this package

if you plan to use something like: from lvk import *

there must be a variable __all__ in this __init__.py, which refers to a
list of all modules that should be imported

as mentioned I'm new to python and this might be wrong but give it a try
 
B

bruno modulix

Lasse said:
I am slowly learning Python and I'm already starting to write some minor
modules for myself. Undoubtedly there are better modules available
either built-in or 3rd party that do the same as mine and much more but
I need to learn it one way or another anyway.

What I'm wondering about is module organization.

I created my own directory for storing my modules and added the full
path to this to PYTHONPATH (Windows XP platform).

This means that "import modulename" works for my modules now.

However, the name of my module is "db" or "database", a module name that
will likely conflict with other modules I might encounter later.

As such, I was thinking of doing the same that the "distutils" set of
modules have done, by creating a subdirectory and storing them there, so
I created a "lvk" directory in that directory of mine and moved the
module in there, but now "import lvk.modulename" doesn't find the module.

What you want is a package:
http://www.python.org/doc/current/tut/node8.html#SECTION008400000000000000000
Is there a trick to this? Do I have to store my own modules beneath
C:\Python24\Lib? or can I use the organization I've tried just with some
minor fixes to make python locate my modules?

briefly put, add an (even empty) __init__.py file in your lvk directory,
and it should work fine (minus potential import problems in your
modules, but that should be easy to fix...)

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top