473,327 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

creating package question

I have a package directory structure as follows

root-
|
Common (contains __init__.py file)
WindowsComponents (contains __init__.py file)
...

I would like modules in the WindowsComponents directory to be able to
import some modules from the Common directory. In my first pass, I was
able to append sys.path ( sys.path.append('../Common') ) in each module
that wants to import from Common, but this feels "clunky". Is there a
"standard"/"best" way to accomplish this?

--ERick

Nov 22 '05 #1
10 1403
I think I have an answer to my own question. In the
WindowsComponents/__init__.py file, I have the following, that feels
like a better answer for the problem. Is there a better answer than
this?

import os, sys
sys.path.append(os.path.join(os.getcwd(), 'Common'))
--ERick

Nov 22 '05 #2
I think I have an answer to my own question. In the
WindowsComponents/__init__.py file, I have the following, that feels
like a better answer for the problem. Is there a better answer than
this?

import os, sys
sys.path.append(os.path.join(os.getcwd(), 'Common'))
--ERick

Nov 22 '05 #3
On Nov 16, er**********@comcast.net wrote:
I have a package directory structure as follows

root-
|
Common (contains __init__.py file)
WindowsComponents (contains __init__.py file)
...

I would like modules in the WindowsComponents directory to be able
to import some modules from the Common directory.
So you now have a "Common" package. And it might contain a "mustard"
module.
In my first pass, I was able to append sys.path (
sys.path.append('../Common') ) in each module that wants to import
from Common, but this feels "clunky".
Agreed. You probably want to avoid messing with sys.path whenever
possible.
Is there a "standard"/"best" way to accomplish this?


So "root" should already be on your sys.path/PYTHONPATH.

Then in say file "root/WindowsComponents/spam.py":

from Common import mustard
...
mustard.attr

More import info from Fredrik:
http://effbot.org/zone/import-confusion.htm

--
_ _ ___
|V|icah |- lliott <>< md*@micah.elliott.name
" " """
Nov 22 '05 #4
On Nov 16, er**********@comcast.net wrote:
I have a package directory structure as follows

root-
|
Common (contains __init__.py file)
WindowsComponents (contains __init__.py file)
...

I would like modules in the WindowsComponents directory to be able
to import some modules from the Common directory.
So you now have a "Common" package. And it might contain a "mustard"
module.
In my first pass, I was able to append sys.path (
sys.path.append('../Common') ) in each module that wants to import
from Common, but this feels "clunky".
Agreed. You probably want to avoid messing with sys.path whenever
possible.
Is there a "standard"/"best" way to accomplish this?


So "root" should already be on your sys.path/PYTHONPATH.

Then in say file "root/WindowsComponents/spam.py":

from Common import mustard
...
mustard.attr

More import info from Fredrik:
http://effbot.org/zone/import-confusion.htm

--
_ _ ___
|V|icah |- lliott <>< md*@micah.elliott.name
" " """
Nov 22 '05 #5

erick_bod...@comcast.net wrote:
I think I have an answer to my own question. In the
WindowsComponents/__init__.py file, I have the following, that feels
like a better answer for the problem. Is there a better answer than
this?

import os, sys
sys.path.append(os.path.join(os.getcwd(), 'Common'))


My solution to this is to use a .pth file. In my site-packages folder
under the python installation, I have a file named infidel.pth. This
file contains the path to the folder where I put all my python source
code (C:\src\py). In your case, you could have the path to your 'root'
folder.

One of my projects is structured like this:

C:\src\py
infidel\
__init__.py
models\
__init__.py
basemodel.py
views\
__init__.py
baseview.py
controllers\
__init__.py

Now the controllers package can do imports like this:

from infidel.models import basemodel
from infidel.views import baseview

The point is that the .pth file in site-packages adds custom paths to
your sys.path

Nov 22 '05 #6

erick_bod...@comcast.net wrote:
I think I have an answer to my own question. In the
WindowsComponents/__init__.py file, I have the following, that feels
like a better answer for the problem. Is there a better answer than
this?

import os, sys
sys.path.append(os.path.join(os.getcwd(), 'Common'))


My solution to this is to use a .pth file. In my site-packages folder
under the python installation, I have a file named infidel.pth. This
file contains the path to the folder where I put all my python source
code (C:\src\py). In your case, you could have the path to your 'root'
folder.

One of my projects is structured like this:

C:\src\py
infidel\
__init__.py
models\
__init__.py
basemodel.py
views\
__init__.py
baseview.py
controllers\
__init__.py

Now the controllers package can do imports like this:

from infidel.models import basemodel
from infidel.views import baseview

The point is that the .pth file in site-packages adds custom paths to
your sys.path

Nov 22 '05 #7
er**********@comcast.net a écrit :
I think I have an answer to my own question. In the
WindowsComponents/__init__.py file, I have the following, that feels
like a better answer for the problem. Is there a better answer than
this?

import os, sys
sys.path.append(os.path.join(os.getcwd(), 'Common'))


Err... if the script is called from somewhere else, this won't work.
replace os.getcwd() with os.path.dirname(os.path.realpath(__file__))

Or better, use a .pth or add the needed path in your PYTHONPATH
Nov 22 '05 #8
er**********@comcast.net a écrit :
I think I have an answer to my own question. In the
WindowsComponents/__init__.py file, I have the following, that feels
like a better answer for the problem. Is there a better answer than
this?

import os, sys
sys.path.append(os.path.join(os.getcwd(), 'Common'))


Err... if the script is called from somewhere else, this won't work.
replace os.getcwd() with os.path.dirname(os.path.realpath(__file__))

Or better, use a .pth or add the needed path in your PYTHONPATH
Nov 22 '05 #9

Bruno Desthuilliers wrote:
er**********@comcast.net a écrit :
I think I have an answer to my own question. In the
WindowsComponents/__init__.py file, I have the following, that feels
like a better answer for the problem. Is there a better answer than
this?

import os, sys
sys.path.append(os.path.join(os.getcwd(), 'Common'))

Err... if the script is called from somewhere else, this won't work.
replace os.getcwd() with os.path.dirname(os.path.realpath(__file__))

Right, I anticipate the script(s) won't be called from elsewhere,
but....
Or better, use a .pth or add the needed path in your PYTHONPATH

THis would be ideal if I could gaurantee that the users (other software
testers) would install the *.pth file.

thanks for all the suggestions.

Nov 22 '05 #10

Bruno Desthuilliers wrote:
er**********@comcast.net a écrit :
I think I have an answer to my own question. In the
WindowsComponents/__init__.py file, I have the following, that feels
like a better answer for the problem. Is there a better answer than
this?

import os, sys
sys.path.append(os.path.join(os.getcwd(), 'Common'))

Err... if the script is called from somewhere else, this won't work.
replace os.getcwd() with os.path.dirname(os.path.realpath(__file__))

Right, I anticipate the script(s) won't be called from elsewhere,
but....
Or better, use a .pth or add the needed path in your PYTHONPATH

THis would be ideal if I could gaurantee that the users (other software
testers) would install the *.pth file.

thanks for all the suggestions.

Nov 22 '05 #11

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

Similar topics

0
by: David M. Wilson | last post by:
Hello! I maintain a small package for talking to the API of BulkSMS.co.uk. I have been adding support for some new features recently, and found myself slightly indecisive over how best to lay...
1
by: Jennifer | last post by:
I've created a DTS package and now I need to distribute it to different servers. I've been looking for a way to automatically/programatically create a DTS package, but have not found anything...
2
by: jcooper | last post by:
Hi all, I'm new to VB.net and have a question about creating the .exe file for my program. In VB6 you could just select 'Make .exe' from the File menu, but everything that I read about...
2
by: jcooper | last post by:
Hi all, I'm new to VB.net and have a question about creating the .exe file for my program. In VB6 you could just select 'Make .exe' from the File menu, but everything that I read about...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.