473,666 Members | 2,225 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Organizing code - import question

Hello,

I am trying to organize some of my code, and am having a little trouble with the
import logic. I find I often have something like:

MyPackage/
Part1/ # wants to use functions in Common/
__init__.py # does "from MyClass1 import MyClass1", etc,...
MyClass1.py
MyClass1a.py # depends on MyClass1
MyClass1b.py # depends on MyClass1

Part2/ # wants to use functions in Common/
__init__.py # does "from MyClass2 import MyClass2", etc,...
MyClass2.py # depends on MyClass1 also, such as containing a list of MyClass1
MyClass2a.py # depends on MyClass2
MyClass2b.py # depends on MyClass2

Common/
__init__.py # does "import fun1,fun2", etc,...
fun1.py
fun2.py

So I have some common utilities that both classes want to access, and I have two
separate class definitions, of which one depends on the other. In MyClass2.py, I
can't seem to do:

import Common.fun1

or

from Part1.MyClass1 import MyClass1
I think I am either missing some syntax/path thing, or I am thinking about the
organization in entirely the wrong way. Currently, as a hack, I am simply copying
the code from Common into the other two directories, and making a link to the Part1
directory in the Part2 so I can import it. There must be a better way, yes?
thanks,

Brian Blais

--
-----------------

bb****@bryant.e du
http://web.bryant.edu/~bblais
May 3 '07 #1
4 1404
On May 3, 8:41 am, Brian Blais <bbl...@bryant. eduwrote:
Hello,

I am trying to organize some of my code, and am having a little trouble with the
import logic. I find I often have something like:

MyPackage/
Part1/ # wants to use functions in Common/
__init__.py # does "from MyClass1 import MyClass1", etc,...
MyClass1.py
MyClass1a.py # depends on MyClass1
MyClass1b.py # depends on MyClass1

Part2/ # wants to use functions in Common/
__init__.py # does "from MyClass2 import MyClass2", etc,...
MyClass2.py # depends on MyClass1 also, such as containing a list of MyClass1
MyClass2a.py # depends on MyClass2
MyClass2b.py # depends on MyClass2

Common/
__init__.py # does "import fun1,fun2", etc,...
fun1.py
fun2.py

So I have some common utilities that both classes want to access, and I have two
separate class definitions, of which one depends on the other. In MyClass2.py, I
can't seem to do:

import Common.fun1

or

from Part1.MyClass1 import MyClass1

I think I am either missing some syntax/path thing, or I am thinking about the
organization in entirely the wrong way. Currently, as a hack, I am simply copying
the code from Common into the other two directories, and making a link to the Part1
directory in the Part2 so I can import it. There must be a better way, yes?

thanks,

Brian Blais

--
-----------------

bbl...@bryant.e du
http://web.bryant.edu/~bblais
It looks like you need __init__.py in MyPackage. Then you can import
starting with MyPackage. For example, you might use one of the
following:

import MyPackage
from MyPackage.Commo n import *
etc

--
Carlos Hanson

May 3 '07 #2
Carlos Hanson wrote:
It looks like you need __init__.py in MyPackage. Then you can import
starting with MyPackage. For example, you might use one of the
following:

import MyPackage
from MyPackage.Commo n import *
etc
that means that MyPackage must be in the sys path too? It doesn't seem like a
contained-module sees the container in any way.
bb
--
-----------------

bb****@bryant.e du
http://web.bryant.edu/~bblais
May 3 '07 #3
En Thu, 03 May 2007 12:41:00 -0300, Brian Blais <bb****@bryant. edu>
escribió:
I am trying to organize some of my code, and am having a little trouble
with the import logic. I find I often have something like:

MyPackage/
Part1/ # wants to use functions in Common/
__init__.py # does "from MyClass1 import MyClass1", etc,...
MyClass1.py
MyClass1a.py # depends on MyClass1
MyClass1b.py # depends on MyClass1

Part2/ # wants to use functions in Common/
__init__.py # does "from MyClass2 import MyClass2", etc,...
MyClass2.py # depends on MyClass1 also, such as containing a
list of MyClass1
MyClass2a.py # depends on MyClass2
MyClass2b.py # depends on MyClass2

Common/
__init__.py # does "import fun1,fun2", etc,...
fun1.py
fun2.py

So I have some common utilities that both classes want to access, and I
have two separate class definitions, of which one depends on the other.
In MyClass2.py, I can't seem to do:

import Common.fun1

or

from Part1.MyClass1 import MyClass1
To be able to do that, MyPackage should be on sys.path
If its *container* (i.e. the directory containing MyPackage, perhaps
site-packages) is already on sys.path, you could prefix all imports with
the package name: import MyPackage.Commo n.fun1, or from MyPackage.Part1
import MyClass1
(Dont forget the __init__.py on MyPackage, to make it a real package)

If you are using Python 2.5, you can use relative imports. Read the
"What's new" document. In MyClass2.py you could use, then: from ..Common
import fun1, or: from ..Part1.MyClass 1 import MyClass1

--
Gabriel Genellina
May 4 '07 #4
On 5/3/07, Brian Blais <bb****@bryant. eduwrote:
Carlos Hanson wrote:
It looks like you need __init__.py in MyPackage. Then you can import
starting with MyPackage. For example, you might use one of the
following:

import MyPackage
from MyPackage.Commo n import *
etc

that means that MyPackage must be in the sys path too? It doesn't seem like a
contained-module sees the container in any way.
That is exactly right. Without being in the sys path, Python does not
know where to look to resolve the import statements.
--
Carlos Hanson
May 4 '07 #5

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

Similar topics

7
1716
by: ToMeK | last post by:
hi! i'm trying to builid web catalog and i'm having problems with organizing my tables. here's what i want: i have main categories, sub_categories and articles like this: CATEGORY_1 SUB_CAT_1 Article11 Article12 (...)
3
4327
by: Jamie | last post by:
Hi, Thanks for the excellent answer to my last question! One more: Does anyone have a method they follow for organizing stylesheets themselves? They seem like they can get bloated and hard to read. Aside from putting all the "h1" rules together, I haven't thought of any way to do it, if it's necessary at all. J.
10
3242
by: Rada Chirkova | last post by:
Hi, at NC State University, my students and I are working on a project called "self-organizing databases," please see description below. I would like to use an open-source database system for implementation and would really appreciate your opinion on whether PostgreSQL is suitable for the project. In general, I am very impressed by the quality of PostgreSQL code and documentation, as well as by the support of the developer community. ...
0
1235
by: netsurfer802 | last post by:
I have a question regarding working with seperate files and organizing these files. The company I'm working for is a small comuter company that calls people that have went to computer trade shows...we have computer based CBTs sent out and learning over the Internet. I'm new in this company and am trying to set up a program that seperates leads that are having not a good response or that we aren't able to contect by phone...and then put...
0
2012
by: Fredrik Strandberg | last post by:
I am trying to figure out how to organize a visual studio vb.net solution to make it possible to execute automated unit tests. I encounter problems when creating stubs. When testing an existing system, for which I am not allowed to change the structure (projects, namespaces and so on) or the implementation (only correcting faults is allowed), I have the following situation: -Project "Handlers" -Class "Rodscanner"
1
1352
by: bratiskovci | last post by:
1.How do I get functions to help me simplify access to the data structure. 2. How do I get functions to encapsulate the various calculations that I have to make. 3. How do I get functions to print out the computed information. import sys subjects = # Trial Data trials = ],
12
1868
by: xkenneth | last post by:
All, I apologize if this is a commonly asked question, but I didn't find anything that answered my question while searching. So what I have right now is a few packages that contain some commonly used functions and another package that contains all of my custom error classes. I want these error classes available to me in all of the other packages in my library. Currently to achieve this at the top of every module file I have the line...
5
2816
by: ivarnelispam | last post by:
Hello all, I'm starting work on what is going to become a fairly substantial Python project, and I'm trying to find the best way to organize everything. The project will consist of: - A few applications - Several small scripts and utilities - Unit tests and small interactive test programs - A number of custom libraries and modules that may be shared and
1
1739
by: eliben | last post by:
Hello, At the moment, I place all the code of my project in a src/ directory, and all the tests in a sibling tests/ directory, so for instance a sample project can look like this: doc/ ... src/ play.py
0
8356
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
8866
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
8781
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8639
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
7385
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...
1
6192
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5663
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
4366
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2769
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

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.