[Tutor] Best way to get root project directory in module search path

boB Stepp robertvstepp at gmail.com
Sat Aug 26 22:27:12 EDT 2017


SunOS 5.10, Python 2.4/2.6.  Again, please forgive any typos as I am
having to manually enter everything.

I must confess that even though I've asked about similar topics in the
past, I still don't think I'm fully "getting it".

I have a project structure where I am developing various programs,
some in Pinnacle HotScript language, some in Perl, some in Python,
some shell scripts.  For the purpose of this question, my simplified
development file structure is:

~/_dev_Scripts
    /ScriptMenuSystem
        __init__.py
        test.py

    /py_utilities
        __init__.py
        test.py

When I am fully happy (Not really! ~(:>)) ) with everything I will
copy everything pertinent from "~_dev_Scripts" into a location like
"/usr/local/.../Scripts/boB_Scripts".  So all code must be able to
handle any change in location from where I am developing it.  On to my
difficulties ...

ScriptMenuSystem/test.py:

-------------------------------------------------
#!/usr/bin/env python

import sys
sys.path.append('..')

import py_utilities.test as t

t.test_print()
-------------------------------------------------

py_utilities/test.py:

-------------------------------------------------
#!/usr/bin/env python

def test_print():
    print 'This is module test.py!'
-------------------------------------------------

And when I run the first script from the ScriptMenuSystem location:

> python test.py
This is module test.py!

So, this works, but seems clunky to me.  I do not want to set
PYTHONPATH as that would only help me, not the other people who will
be using my programs.  I have read of including a .pth file with a
contained list of locations that should be part of the module search
path, but is this the best way for my use case?  And can it be written
in such a way that I do not have to edit it when I copy and move the
code to its new location?  As I already have these empty __init__.py
files, is there something I can add to these files to solve the
situation and make the project root directory part of the module
search path?

TIA!

-- 
boB


More information about the Tutor mailing list