473,498 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
Oct 10 '07 #1
4 2164
stef mientki wrote:
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
Oct 10 '07 #2
Larry Bates wrote:
stef mientki wrote:
>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.

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

-Larry
Oct 10 '07 #3
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]
stef mientki wrote:
Larry Bates wrote:
>stef mientki wrote:
>>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.

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

>-Larry
Oct 11 '07 #4
[david] wrote:
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
Sc***********@Acm.Org
Oct 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2856
by: Josef Sachs | last post by:
Is Andrew Kuchling's regex-to-re HOWTO available anywhere? I've found the following (dead) links on various Web pages: http://py-howto.sourceforge.net/regex-to-re/regex-to-re.html...
4
2137
by: DraguVaso | last post by:
Hi, In my application I receive a Byte Stream (Dim bytFile() As Byte) which contains a jpeg-picture, which I want to display in a picturebox. I want to display it directly from the bytfile()...
4
1750
by: Vlady | last post by:
Hello. I recently built an ASP.NET "financial" application. I got to the point where I have a HTML template (a contract) which needs to be filled with various data from the database. I put a...
10
1428
by: Rich | last post by:
Greetings, Just starting out with .net. I have a vb.net app where I connect to an Access mdb. I use a connection component from the tool box (conn1), a data adapter component from the toolbox...
10
2838
by: Peter | last post by:
Hi, how can I do this (I don't really want to do this but what I do want the same function name but have different return types and the compiler keeps saying "public function .... they differ only...
2
1708
by: Frank | last post by:
Hi, I have an vb6 app that I want to port to .net. I tried the wizard but it doesn't convert it all. In my app I have a form with a datagrid on it. If a user doubleclicks on a row a form is shown...
0
3211
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
4
3583
by: shonend | last post by:
I am trying to extract the pattern like this : "SUB: some text LOT: one-word" Described, "SUB" and "LOT" are key words; I want those words, everything in between and one word following the...
0
1107
by: AMDRIT | last post by:
Hello Every, Today I am exploring async operations. In my scenario, I have a windows form with a webbrowser control. The exercise reads a starting webpage and populates a Queue with additional...
6
10536
by: fnoppie | last post by:
Hi, I am near to desperation as I have a million things to get a solution for my problem. I have to post a multipart message to a url that consists of a xml file and an binary file (pdf)....
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5462
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4908
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4588
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1417
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
290
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.