473,597 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

creating package question

I have a package directory structure as follows

root-
|
Common (contains __init__.py file)
WindowsComponen ts (contains __init__.py file)
...

I would like modules in the WindowsComponen ts 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 1423
I think I have an answer to my own question. In the
WindowsComponen ts/__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(o s.getcwd(), 'Common'))
--ERick

Nov 22 '05 #2
I think I have an answer to my own question. In the
WindowsComponen ts/__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(o s.getcwd(), 'Common'))
--ERick

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

root-
|
Common (contains __init__.py file)
WindowsComponen ts (contains __init__.py file)
...

I would like modules in the WindowsComponen ts 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/WindowsComponen ts/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.ellio tt.name
" " """
Nov 22 '05 #4
On Nov 16, er**********@co mcast.net wrote:
I have a package directory structure as follows

root-
|
Common (contains __init__.py file)
WindowsComponen ts (contains __init__.py file)
...

I would like modules in the WindowsComponen ts 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/WindowsComponen ts/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.ellio tt.name
" " """
Nov 22 '05 #5

erick_bod...@co mcast.net wrote:
I think I have an answer to my own question. In the
WindowsComponen ts/__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(o s.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...@co mcast.net wrote:
I think I have an answer to my own question. In the
WindowsComponen ts/__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(o s.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**********@co mcast.net a écrit :
I think I have an answer to my own question. In the
WindowsComponen ts/__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(o s.getcwd(), 'Common'))


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

Or better, use a .pth or add the needed path in your PYTHONPATH
Nov 22 '05 #8
er**********@co mcast.net a écrit :
I think I have an answer to my own question. In the
WindowsComponen ts/__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(o s.getcwd(), 'Common'))


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

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

Bruno Desthuilliers wrote:
er**********@co mcast.net a écrit :
I think I have an answer to my own question. In the
WindowsComponen ts/__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(o s.getcwd(), 'Common'))

Err... if the script is called from somewhere else, this won't work.
replace os.getcwd() with os.path.dirname (os.path.realpa th(__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

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

Similar topics

0
1222
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 out the modules internally. I have a 'BulkSMS.Exceptions' module, which contains a base class and (currently) all derived exceptions that my package throws. This has been fine up until now, as the exceptions were generated by only one
1
5884
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 definite. From the DTS package itself, I see where I can save the dts package as a structured file, with the name XXXXX.dts. Once I have that DTS file, how dow I turn it back into a dts package in Enterprise manager? I don't want to have to...
2
343
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 deploying an application in VB.net talks about creating an installer file. The application I created is very small (just a sample) and I don't want the user to have to install the program, just run an executable. Is there any way to do this in VB.net? ...
2
1838
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 deploying an application in VB.net talks about creating an installer file. The application I created is very small (just a sample) and I don't want the user to have to install the program, just run an executable. Is there any way to do this in VB.net? ...
0
7885
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8272
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8258
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6687
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5428
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3882
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3923
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2403
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 we have to send another system
0
1233
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.