[Tutor] Import from project's lib directory?

leam hall leamhall at gmail.com
Fri Aug 29 15:07:06 CEST 2014


On Thu, Aug 28, 2014 at 6:39 PM, Cameron Simpson <cs at zip.com.au> wrote:
> On 28Aug2014 22:36, Alan Gauld <alan.gauld at btinternet.com> wrote:
>>
>> On 28/08/14 19:03, leam hall wrote:
>>>
>>> python 2.4.3 on Red Hat Linux.
>>>
>>> I'm starting a project and want to break the scripts into "input",
>>> "output", and "ddl". I'd like to have a "lib" library directory for
>>> local modules. The library directory would be at the same level as the
>>> others.
>>>
>>> How do I get the library modules?
>>
>>
>> Add lib to the sys.path list?
>>
>> That may not be viable if you need it to be portable across systems,
>> although using os.getcwd to locate the current folder or an
>> environment variable to store the app root folder might be
>> options there.

The code will be imported to other machines so whatever I'm doing must
be portable. Right now my code looks like this:

###
## lib/mymodule.py

#!/usr/bin/env python

def howdy():
        print("in mymodule")

###
## input/test_lib.py

#!/usr/bin/env python

from ..lib import mymodule
mymodule.howdy()

###
## Directory tree:
.
./input
./input/__init.py__
./input/test_lib.py
./lib
./lib/mymodule.py
./lib/__init.py__
./output


## In input, running test_lib.py
 ./test_lib.py
Traceback (most recent call last):
  File "./test_lib.py", line 3, in <module>
    from ..lib import mymodule
ValueError: Attempted relative import in non-package


## the __init.py__ files are empty.


-- 
Mind on a Mission


More information about the Tutor mailing list