473,394 Members | 2,090 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,394 software developers and data experts.

need clarification with import statements

Hi,
I've got the following hierarchy:

mainprog/
__init__.py
prog.py
utils/
__init__.py
myutils.py
others/
__init__.py
myothers.py

Inside prog.py I need to have full access to myutils.py and
myothers.py;
Inside myutils.py, I need to access two classes from myothers.py (ie
myotherClass1 and myotherClass2);
Inside myothers.py, I need some functions of myutils.py (ie
myutils_func1 and myutils_func2);

Do you have some hints please ? I'm trying to avoid name conflicts, so
I haven't used "from ... import *".
I suspect ther's something wrong, but I think I really don't understand
what's going on internally (no errors (for the moment !))

Inside prog.py : from utils.myutils import myotherClass1, myotherClass2
+ some functions of myutils I need
Inside myutils.py : from other.myothers import myotherClass1,
myotherClass2
Inside others.py : import utils.myutils

Thanks.

Dec 14 '06 #1
2 1103

Tool69 wrote:
Hi,
I've got the following hierarchy:

mainprog/
__init__.py
prog.py
utils/
__init__.py
myutils.py
others/
__init__.py
myothers.py

Inside prog.py I need to have full access to myutils.py and
myothers.py;
Inside myutils.py, I need to access two classes from myothers.py (ie
myotherClass1 and myotherClass2);
Inside myothers.py, I need some functions of myutils.py (ie
myutils_func1 and myutils_func2);

Do you have some hints please ?
1. Your directory/package hierarchy is far too complicated. Flatten it.

2. You have circular references: the others module will import from
utils, but utils wants to import from others. This is prima facie
evidence that your modules are not structured properly. Rule 1: Modules
should contain /related/ classes and functions. Rule 2: A graph of what
imports what should not have loops.

Consider combining others and utils -- does that make sense?

Another alternative: split out those "some functions of myutils.py (ie
myutils_func1 and myutils_func2)" into a fourth module. This module
might be the nucleus of a tool-kit of miscellaneous functions that you
might import in /any/ app -- in that case, move it outside the current
package.

HTH,
John

Dec 15 '06 #2
Hi John,
1. Your directory/package hierarchy is far too complicated. Flatten it.
Ok.
2. You have circular references: the others module will import from
utils, but utils wants to import from others. This is prima facie
evidence that your modules are not structured properly
Yes, that's why I asked the question in fact, circular references are a
bit hazardous.
>. Rule 1: Modules should contain /related/ classes and functions.
Rule 2: A graph of what imports what should not have loops.

Consider combining others and utils -- does that make sense?
Yes, I can combine them as they need eachother.
Another alternative: split out those "some functions of myutils.py (ie
myutils_func1 and myutils_func2)" into a fourth module. This module
might be the nucleus of a tool-kit of miscellaneous functions that you
might import in /any/ app -- in that case, move it outside the current
package.
Good idea indeed,
Thanks for all,
6TooL9

Dec 15 '06 #3

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

Similar topics

10
by: Jeff Wagner | last post by:
I am in the process of learning Python (obsessively so). I've been through a few tutorials and read a Python book that was lent to me. I am now trying to put what I've learned to use by rewriting...
2
by: Aahz | last post by:
There were some posts recently discussing whether it's poor style to put import statements inside functions. I recently got reminded that there's one very good reason to avoid it: Python has an...
8
by: Sai Kit Tong | last post by:
In the article, the description for "Modiy DLL That Contains Consumers That Use Managed Code and DLL Exports or Managed Entry Points" suggests the creation of the class ManagedWrapper. If I...
1
by: Shane Hathaway | last post by:
Let's talk about the problem I really want help with. I brought up a proposal earlier, but it was only half serious. I realize Python is too sacred to accept such a heretical change. ;-) ...
4
by: Brie_Manakul | last post by:
I need to set up an if else to show different weather scripts based on the city selection they choose. Any help on this would be great. Thanks! <%@ page language="java" import="java.util.*,...
7
helpwithcode
by: helpwithcode | last post by:
Hi people, I am just learning java.I have been creating a project which involves JDBC Connectivity.I find that the statements, String string_dob=text_dob.getText(); //Converting string to...
5
by: W. Watson | last post by:
Is there a single source that explains these statements? ------------------------------ from Tkinter import * from Numeric import * import Image import ImageChops import ImageTk import time...
2
by: ravir | last post by:
Hi, I am new to this group. I am working in Perl and shellscripts. I have a clarification regarding perl grep and pattern matching. I am writing a perl script to automate the process of code...
3
by: notnorwegian | last post by:
import Tkinter from Tkinter import * i have a program where if i comment out either of those import- statements i get an error. i thought they meant the same thing and from was supposed to be...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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
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,...
0
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
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...

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.