0

I have the following structure:

projectname/
  somefolder/
    somecode.py
    shared.py
  somefolder2/
    somecode2.py
    shared.py

There is a __init__.py file in each folder.

Here "shared.py" is exactly the same so I have to copy and paste it between somefolder and somefolder2.

I want to do the following:

1) Add a shared folder and put the shared.py in it:

shared/
    shared.py

2) Reference this in somecode.py and somecode2.py. I have tried:

from projectname.shared.shared import *
from ..shared.shared import *

I see for instance that boto (https://github.com/boto/boto/tree/develop/boto) nicely reference any path from anywhere in the project simply using paths like "boto.etc" so it must be possible.

EDIT: Changed title to include "Windows 7" and also note that I have read all the other explanations but they either suggest some hack in code or generally suggest many ways to do this in path, pythonpath and so on. I want one single solution for Python 2.7 on Windows 7 that simply works.

Update: Got a bit further by:
1. Adding python path as suggested in the approved answer here:
How to add to the pythonpath in windows 7?
2. Ensuring __init__.py is properly set.
Status now is that I can reference the project and it's folders but not the code file in the folder. So:

from projectname.shared.shared import *

Here projectname.shared is found but not the last part which is the code-file.

Thanks, Marius

Community
  • 1
  • 1
Marius Lian
  • 523
  • 7
  • 15

1 Answers1

1

You need to make sure projectname is in your sys.path, either by modifying the list, creating a .pth file somewhere in your sys.path locations, or setting your working directory to projectname. There may be other ways but those are the easiest ones.

mhlester
  • 22,781
  • 10
  • 52
  • 75