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

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.edu
http://web.bryant.edu/~bblais
May 3 '07 #1
4 1390
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.edu
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.Common 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.Common 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.edu
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.Common.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.MyClass1 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.Common 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
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...
3
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...
10
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...
0
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...
0
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...
1
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...
12
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...
5
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...
1
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/ ......
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.