473,385 Members | 1,570 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,385 software developers and data experts.

Modules, Packages and Developer Confusion. Oh My!

I think I'm still missing something in how python is handling packages
and it's mixing me up. I have a package with three files (modules?)
like so:

OPS:\
__init__.py
model.py
search.py

To hide more details of the package structure, I import model and
search inside of __init__. It also seemed like a good idea to define a
global function that creates a database connection and I added it to
__init__.py. Thus, I have:
from model import *
from search import *

def create_connection():
# details are unimportant for this example
When I try to use the create_connection function in model, I get errors
when I use it as a global function ( just create_connection()). The
only way to resolve the error was to import OPS inside of model and use
OPS.create_connection(). This doesn't seem natural. If model is part of
OPS, why do I need to tell python to import OPS and use this function
from OPS? I can see doing that from the outside world, but inside?

Any clarification would be greatly appreciated.

Nov 10 '06 #1
2 1085
dh****@gmail.com wrote:
from model import *
from search import *

def create_connection():
# details are unimportant for this example
When I try to use the create_connection function in model, I get errors
when I use it as a global function ( just create_connection()).
importing everything from "model" into "OPS" isn't the same thing as
importing everything from "OPS" into "model".

</F>

Nov 10 '06 #2
dh****@gmail.com wrote:
I think I'm still missing something in how python is handling packages
and it's mixing me up. I have a package with three files (modules?)
like so:

OPS:\
__init__.py
model.py
search.py

To hide more details of the package structure, I import model and
search inside of __init__. It also seemed like a good idea to define a
global function that creates a database connection and I added it to
__init__.py. Thus, I have:
from model import *
from search import *

def create_connection():
# details are unimportant for this example
When I try to use the create_connection function in model, I get errors
when I use it as a global function ( just create_connection()). The
only way to resolve the error was to import OPS inside of model and use
OPS.create_connection(). This doesn't seem natural. If model is part of
OPS, why do I need to tell python to import OPS and use this function
from OPS? I can see doing that from the outside world, but inside?

Any clarification would be greatly appreciated.
The def statement binds the name create_connection in the model module's
namespace to the function it defines. So you write EITHER

import model # puts model in the current namespace
conn = model.create_connection( ... )

OR

from model import * # copies model's namespace into current
conn = create_connection( ... )

Hope this helps.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Nov 10 '06 #3

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

Similar topics

0
by: Nick Coghlan | last post by:
Anyone playing with the CPython interpreter's new command line switch might have noticed that it only works with top-level modules (i.e. scripts that are directly on sys.path). If the script is...
15
by: Nick Coghlan | last post by:
Python 2.4's -m command line switch only works for modules directly on sys.path. Trying to use it with modules inside packages will fail with a "Module not found" error. This PEP aims to fix that...
3
by: Dennis Clark | last post by:
This is a total newb question, you have been warned... I've been all over the www.python.org site and googled, but I've not found just how to add new modules. I've tried setting PYTHONPATH,...
4
by: Fabian Braennstroem | last post by:
Hi, I am pretty new to python and will use it mainly in combination with scientific packages. I am running ubuntu breezy right now and see that some packages are out of date. Do you have any...
17
by: mohan | last post by:
Hi Guys, I've been using the following IDE, "Pythonwin - Python IDE and GUI Framework for Windows. Copyright 1994-2001 Mark Hammond " With respect to my work, I had created my own modules...
3
by: Tinka | last post by:
Hi, I'm having trouble with my new directory hierarchy. My little python programme worked as long as I had all my modules in one big file. I have decided now that this was just to messy and I...
7
by: tinnews | last post by:
I'm just beginning to create some python modules for my own use and I'm wondering where to put them. Initially I have put them in $HOME/bin and I have set PYTHONPATH to point to them there. It...
5
by: John Ladasky | last post by:
Hi folks, Running Python 2.5 on both a Windows XP laptop, and an Ubuntu Linux 7.04 desktop. I've gotten tired of maintaining multiple copies of my personal modules that I use over and over. ...
16
by: ssecorp | last post by:
Is there a way to view all the modules I have available for import from within Python? Like writing in the interpreter: import.modules Also, is there anything like Cpan for Python?
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...
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.