By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,824 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,824 IT Pros & Developers. It's quick & easy.

creating package question

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.