How to simulate packages?

D

Daniel

Hello,

I have a project that I've decided to split into packages in order to
organize my code better. So what I have looks something like this

src
-module1
-mod1_file.py
-module2
-mod2_file.py

Everytime I run mod2_file.py I need to import mod1_file.py. Right now
I'm using an ugly little thing to do my import (see below). Isn't
there some python mechanism to handle packages?

import os, sys
# add packages to path then finish importing
for dir in ['module1','module2']:
# how about this ugly thing to find adjacent directories for
imports
sys.path.append('\\'.join(os.path.dirname(__file__).split('\\')
[:len(os.path.dirname(__file__).split('\\'))-1]) + "\\" + dir)

Any and all suggestions appreciated. Thanks in advance.

Daniel
 
G

Gabriel Genellina

I have a project that I've decided to split into packages in order to
organize my code better. So what I have looks something like this

src
-module1
-mod1_file.py
-module2
-mod2_file.py

Everytime I run mod2_file.py I need to import mod1_file.py. Right now
I'm using an ugly little thing to do my import (see below). Isn't
there some python mechanism to handle packages?

Sure - Python *has* packages, you don't have to "simulate" them.
Read http://docs.python.org/tut/node8.html#SECTION008400000000000000000
 
J

Jason Scheirer

Sure - Python *has* packages, you don't have to "simulate" them.
Readhttp://docs.python.org/tut/node8.html#SECTION008400000000000000000

You can use relative imports, so in mod2_file.py you could

import ..module1.mod1_file as mod1

To pull in a dependent submodule. Also, I think you are thinking about
__init__.py?

package/
__init__.py
mod1/
mod1.py
mod2/
mod2.py

And in your __init__.py you issue something like

import mod1
import mod2

if you need them loaded as soon as your package is imported, but you
may omit that and import piecemeal in your scripts as each one needs
functionality:

from mypackage.mod2 import just_this_one_function

Etc etc etc. But really if you only have a single Python file living
in each subdirectory, you are likely Doing It Wrong and your code
might just make more sense as:

package/
__init__.py
intuitively_named_submodule1.py
intuitively_named_submodule2.py
intuitively_named_submodule3.py

Where your intuitively named submodules are all logical groupings of
functionality. Remember that they don't ALL get imported right away,
in fact, only __init__.py is imported by default, so if that's missing
or doesn't import the other submodules by default, you have to
explicitly

import package.intuitively_named_submoduleN

each time you want to import a submodule.

Modules and packages: http://docs.python.org/tut/node8.html
Relative imports: http://www.python.org/dev/peps/pep-0328/
 
B

Bruno Desthuilliers

Daniel a écrit :
Hello,

I have a project that I've decided to split into packages in order to
organize my code better. So what I have looks something like this

src
-module1
-mod1_file.py
-module2
-mod2_file.py

Everytime I run mod2_file.py I need to import mod1_file.py. Right now
I'm using an ugly little thing to do my import (see below). Isn't
there some python mechanism to handle packages?

cf Gabriel's answer wrt/ Python's packages.

import os, sys
# add packages to path then finish importing
for dir in ['module1','module2']:
# how about this ugly thing to find adjacent directories for
imports
sys.path.append('\\'.join(os.path.dirname(__file__).split('\\')
[:len(os.path.dirname(__file__).split('\\'))-1]) + "\\" + dir)

Any and all suggestions appreciated. Thanks in advance.


A suggestion: os.path is about os independance when it comes to
filesystem. Please read the whole doc for this module, and learn how to
avoid writing system dependant code.
 
D

Daniel

Sure - Python *has* packages, you don't have to "simulate" them.
Readhttp://docs.python.org/tut/node8.html#SECTION008400000000000000000

Thanks for the link. That's exactly what I was looking for. Not sure
why I didn't find it on Google.

Much appreciated.

Daniel
 
D

Daniel

Daniel a écrit :
I have a project that I've decided to split into packages in order to
organize my code better.  So what I have looks something like this
src
  -module1
    -mod1_file.py
  -module2
    -mod2_file.py
Everytime I run mod2_file.py I need to import mod1_file.py.  Right now
I'm using an ugly little thing to do my import (see below).  Isn't
there some python mechanism to handle packages?

cf Gabriel's answer wrt/ Python's packages.
import os, sys
# add packages to path then finish importing
for dir in ['module1','module2']:
    # how about this ugly thing to find adjacent directories for
imports
    sys.path.append('\\'.join(os.path.dirname(__file__).split('\\')
[:len(os.path.dirname(__file__).split('\\'))-1]) + "\\" + dir)
Any and all suggestions appreciated.  Thanks in advance.

A suggestion: os.path is about os independance when it comes to
filesystem. Please read the whole doc for this module, and learn how to
avoid writing system dependant code.

Good suggestion. Thank you.
 

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

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top