1

I have a project setup looks like this:

Base project

/some_disk/some_folder/
|-- project/
|   |-- package/
|   |   |-- src/
|   |   |   |-- file_a.py
|   |   |   |-- file_b.py

Extension project

/some_other_disk/some_folder/
|-- project/
|   |-- package/
|   |   |-- src/
|   |   |   |-- file_c.py
|   |   |   |-- file_d.py

Then I have a third project, in which I would like to be able to use both mopdules file_a and file_c.

In that third project, I have setup my Python path like this

PYTHONPATH=$PYTHONPATH:/some_disk/some_folder:/some_other_disk/some_folder

Then, to import the files, I have this in my main module:

import project.module.src.file_a
import project.module.src.file_c

This, however, only makes me able to import one of the modules, and having an module not found error on the other one.

Can I make this work using this project structure? Or will Python always only look into one of the "main"-modules and consider the sub-module not found if it's not in there?

EDIT: The project makes use of Python 2.6

Christoffer Karlsson
  • 4,539
  • 3
  • 23
  • 36
  • Even if you can trick Python into letting you have 2 different packages with the exact same name, this sounds like a bad idea. Why do you want to do this? – Wooble Aug 01 '13 at 12:54
  • Basically, the main project is much bigger and contains a lots of data and has the project.module-layout, and the extension project is more like "plugin" project that, through the third project, can be used. Making the extension project a part of the main project isn't an option either. It has been added with symlinks in Unix before, but I want to get rid of them as they unintentionally might be added to the main project with the VCS-system. – Christoffer Karlsson Aug 01 '13 at 13:03
  • 2
    @Wooble it's actually a common thing to want. The questioner wants **namespace packages**. And [this](http://stackoverflow.com/questions/1675734/how-do-i-create-a-namespace-package-in-python) is a relevant question for that. – Wessie Aug 01 '13 at 13:22
  • @Wessie: Thanks, that link helped me. – Christoffer Karlsson Aug 02 '13 at 05:31

3 Answers3

7

Create a package file __init__.py in each of your src directories. They should contain the following two lines. See this documentation for details. This solution works on Python 2.6 and is the canonical solution.

from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
eel ghEEz
  • 1,186
  • 11
  • 24
Jonathan
  • 2,635
  • 3
  • 30
  • 49
  • Thanks, that worked! However, do I need to put it in both Base project and the Extended project? A little testing showed that it seems like it's only needed for the project being first in the PYTHONPATH. – Christoffer Karlsson Aug 02 '13 at 05:30
  • 1
    It is safest to put in both. That way you won't get bit if you wind up importing the Extended module before the Base module. – Jonathan Aug 03 '13 at 01:03
0

This will make python search in your current directory/standard directories first, and for the second one, python will search in your pathtofile_c first before standard directories.

import project.module.src.file_a  #<--- here it searches some_disk first
sys.path.insert(0,'pathtofile_c') #<--- Changes your PYTHONPATH - inserts some_other_disk before standard directories
import project.module.src.file_c  #<--- here it searches some_other_disk first

This should clear python's confusion.

sihrc
  • 2,728
  • 2
  • 22
  • 43
-2

You need to have __init__.py files within those directories to make python treat then as a package instead of plain directories.

Refer to this discussion to learn more about init.py files.

Note:I have edited my previous answer by removing the irrelevant content based on the discussion with the poster of the query.

Community
  • 1
  • 1
Prahalad Deshpande
  • 4,709
  • 1
  • 20
  • 22