howto add a sub-directory to the searchpath / namespace ?

S

stef mientki

hello,

my program has become a bit large,
and now I want to split the files over several subdirectories.
So in the example shown below, I just moved the files f1.py and f2.py to
a deeper subdirectory.

basedirectory\
mainfile.py
file1.py
file2.py
subdir1\
__init__.py
f1.py
f2.py

Now I don't want (even can't) change my program,
to change imports from
from f1 import something
into
from subdir1.f1 import something
simply because f1.py and f2.py are python files dropped by users
and I do not know on forehand what will be dropped.

I looked into the description of __init__.py,
in the hope I could make f1.py and f2.py available as if they were in
the basedirectory,
but i couldn't find a way.

Is there a way to make f1.py and f2.py available as if they were located
in the base directory,
without knowing their names (so in general all py-files in the subdir1) ??

thanks,
Stef Mientki
 
L

Larry Bates

stef said:
hello,

my program has become a bit large,
and now I want to split the files over several subdirectories.
So in the example shown below, I just moved the files f1.py and f2.py to
a deeper subdirectory.

basedirectory\
mainfile.py
file1.py
file2.py
subdir1\
__init__.py
f1.py
f2.py

Now I don't want (even can't) change my program,
to change imports from
from f1 import something
into
from subdir1.f1 import something
simply because f1.py and f2.py are python files dropped by users
and I do not know on forehand what will be dropped.

I looked into the description of __init__.py,
in the hope I could make f1.py and f2.py available as if they were in
the basedirectory,
but i couldn't find a way.

Is there a way to make f1.py and f2.py available as if they were located
in the base directory,
without knowing their names (so in general all py-files in the subdir1) ??

thanks,
Stef Mientki
Put basedirectory\subdir in the PYTHONPATH environment variable or

os.path.append(r'basedirectory\subdir1')

in the body of your program.

-Larry
 
S

stef mientki

Larry said:
Put basedirectory\subdir in the PYTHONPATH environment variable or

os.path.append(r'basedirectory\subdir1')

in the body of your program.
thanks Larry,
after a bit of fiddling, I think "os." must be "sys."
and basedirectory shouldn't be in.
so it becomes
sys.path.append ( r'subdir1' )
But what the .. is that "r" in front of the appended path ?

cheers,
Stef Mientki
 
D

[david]

r" indicates a 'regular expression' string, normally
called a raw string. It means that \ characters are
treated using the regex syntax rather than the c syntax.

In the regex syntax, \ characters are escape characters
only at the end of the string, which allows you to
easily use Windows directory notation as long as you
don't need to end a path with a \

sys.path.append("c:\\code\\newcode")
sys.path.append(r"c:\code\newcode")

The os.path module contains additional path handling methods.

[david]
 
S

Scott David Daniels

r" indicates a 'regular expression' string, normally
called a raw string. It means that \ characters are
treated using the regex syntax rather than the c syntax.

This is an incredibly creative answer , if not fastidiously
correct. Raw strings simply disable the special effects of
backslash (\) within the string (except that it cannot be
the final character of a quoted string even if it a raw string
for technical lexer (tokenizer) simplicity reasons). The regex
syntax is used on the resulting string constant if (and only if)
the string is passed to the regular expression functions as a
regex pattern parameter.

-Scott David Daniels
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top