1

I am pretty new to Python so it might sound obvious but I haven't found this everywhere else

Say I have a application (module) in the directoy A/ then I start developing an application/module in another directory B/

So right now I have

source/
 |_A/
 |_B/

From B I want to use functions are classes defined in B. I might eventually pull them out and put them in a "misc" or "util" module.

In any case, what is the best way too add to the PYTHONPATH module B so A can see it? taking into account that I will be also making changes to B.

So far I came up with something like:

def setup_paths():
 import sys
 sys.path.append('../B')

when I want to develop something in A that uses B but this just does not feel right.

asked Jun 7, 2013 at 9:20
2
  • are you sure that this is not working?it does for me?or is it just the error in file name?you use _B Commented Jun 7, 2013 at 9:41
  • it is working. I am asking for the "best" or "right" way to do it. Commented Jun 10, 2013 at 10:42

3 Answers 3

3

Normally when you are developing a single application your directory structure will be similar to

src/
 |-myapp/
 |-pkg_a/
 |-__init__.py
 |-foo.py
 |-pkg_b/
 |-__init__.py
 |-bar.py
 |-myapp.py

This lets your whole project be reused as a package by others. In myapp.py you will typically have a short main function.

You can import other modules of your application easily. For example, in pkg_b/bar.py you might have

import myapp.pkg_a.foo

I think it's the preferred way of organising your imports.

You can do relative imports if you really want, they are described in PEP-328.

import ..pkg_a.foo

but personally I think, they are a bit ugly and difficult to maintain (that's arguable, of course).

Of course, if one of your modules needs a module from another application it's a completely different story, since this application is an external dependency and you'll have to handle it.

answered Jun 7, 2013 at 9:38
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Krelagin, thanks for answering. But this is no what I was asking. I am taking that pkg_a and pkg_b are two different modules that will eventually be used by other application. Yet, I have to develop them concurrently and possible pkg_b will depend on functionality provided by pkg_a.
0

I would recommend using the imp module

import imp
imp.load_source('module','../B/module.py')

Else use absolute path starting from root

def setup_paths():
 import sys
 sys.path.append('/path/to/B')
answered Jun 7, 2013 at 9:35

Comments

0

Add proper folder to PYTHONPATH

First, create the root directory for package that you create. Let's call it project. Subdirs of project are your apps, that is A and B. Now you have to add the parent directory of project to PYTHONPATH.

Refering modules inside the package.

Let's say you have other_app.py in B, so in A/app_name.py you import it like this:

from project.B.other_app import *

Or if you want to have all symbols in other_app namespace import like this:

from project.B import other_app

Nice way of creating launchers and dynamically change PYTHONPATH

If you want to create universal app launchers for Python which work even when you move your package to other PC/dir you need some solution to dynamically add package parent directory to PYTHONPATH. Here is my solution to this case (for Linux, but you can also translate simple scripts to Windows if you google a bit :) )

In the same folder in which you created project create pwd_to_pythonpath.sh, short bash script:

#!/bin/bash
export PYTHONPATH=$PYTHONPATH:`pwd`

Then create launcher for your A and B, for example A.sh would look like:

#!/bin/bash
source pwd_to_pythonpath.sh
python -m project.A.app_name

The app_name should be the same as module file name in A folder (app_name.py in this case)

answered Jun 7, 2013 at 9:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.