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

Simple Python Project Structure

Hi,

I recently wrote a fairly complex project in python. It works great
and it was completed fairly quickly thanks to python!

Anyways, I am in the process of cleaning the code/directory and I had
a simple question....

How do create my own modules and import them? Right now it works but
they all have to be in the same directory. For example,

project/
.....util/
.....config/
.....tests/
.....start.py

I have a module in util/console.py, how do I import that. In start.py
I have:

from project.util.console import filetest

but I get an ImportError.

Thanks!

Amit
Oct 11 '08 #1
3 2244
On Oct 10, 7:17*pm, amit <amit.ut...@gmail.comwrote:
How do create my own modules and import them? Right now it works but
they all have to be in the same directory. For example,

project/
....util/
....config/
....tests/
....start.py
You need an __init__.py file (it doesn't matter if it's empty) in the
directory to turn it into a package.

http://www.python.org/doc/2.5.2/tut/...00000000000000

HTH.

....
Jay Graves
Oct 11 '08 #2
On Oct 10, 9:26*pm, jay graves <jaywgra...@gmail.comwrote:
On Oct 10, 7:17*pm, amit <amit.ut...@gmail.comwrote:
How do create my own modules and import them? Right now it works but
they all have to be in the same directory. For example,
project/
....util/
....config/
....tests/
....start.py

You need an __init__.py file (it doesn't matter if it's empty) in the
directory to turn it into a package.

http://www.python.org/doc/2.5.2/tut/...08400000000000...
I'm wondering if this is one of the few cases where Python's choice to
be explicit causes more trouble than it's worth. The official
reasoning is:
'''
The __init__.py files are required to make Python treat the
directories as containing packages; this is done to prevent
directories with a common name, such as "string", from unintentionally
hiding valid modules that occur later on the module search path.
'''

Is this a real problem or a speculation ? I would guess that it's at
least as likely for a newbie to create a "string.py" module than have
an irrelevant "string" subdirectory under a code directory tree.
Having to create an empty file as a flag to denote a package doesn't
seem very pythonic.

George
Oct 11 '08 #3
George Sakkis <ge***********@gmail.comwrites:
'''
The __init__.py files are required to make Python treat the
directories as containing packages; this is done to prevent
directories with a common name, such as "string", from
unintentionally hiding valid modules that occur later on the module
search path.
'''

Is this a real problem or a speculation ? I would guess that it's at
least as likely for a newbie to create a "string.py" module than
have an irrelevant "string" subdirectory under a code directory
tree. Having to create an empty file as a flag to denote a package
doesn't seem very pythonic.
The underlying problem, of course, is that Python's ‘import’ statement
doesn't let the programmer distinguish between “import from the
system search path” versus “import from this local package”. If it
did, you could say “import ‘string’ from the system search path”
without worrying about what happens when a ‘string’ module or package
is also located in your local package.

Fortunately, this is already addressed with absolute imports versus
relative imports <URL:http://www.python.org/dev/peps/pep-0328>. See
the “Timeline” section in that PEP for what import behaviour to
expect under different Python versions.

--
\ “Contentment is a pearl of great price, and whosoever procures |
`\ it at the expense of ten thousand desires makes a wise and |
_o__) happy purchase.” —J. Balguy |
Ben Finney
Oct 11 '08 #4

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

Similar topics

20
by: Daniel R. Smorey Jr. | last post by:
I'm looking for a good place for Python documentation. I'm really lost on why it's so hard to find anything when it comes to me looking up a particular function in Python. My example would be the...
4
by: Dave Benjamin | last post by:
The recent conversation on prototype-based OOP and the Prothon project has been interesting. I've been playing around with the idea for awhile now, actually, since I do a lot of programming in...
29
by: Maurice LING | last post by:
Hi, I remembered reading a MSc thesis about compiling Perl to Java bytecodes (as in java class files). At least, it seems that someone had compiled scheme to java class files quite successfully....
0
by: Steve | last post by:
Hi list, At work, we would shortly be starting a new project that would be almost entirely (90%) written in python. Our development as well as deployment would happen on linux (fedora and RHEL to...
10
true911m
by: true911m | last post by:
This is a simple walkthrough to get PyInstaller up and running. I decided to give PI a try, because it claims to be more selective about what it bundles into its executable files by default, and...
3
by: marco Starglider | last post by:
hi, i have a very large project in visual studio2005->visual c++ in windowsxp. i'd like to port it, or my next project, over to python. is this possible without rewriting all my code? are...
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...
2
by: Rex | last post by:
Hello, I am a Python programmer facing my first small XML project. I am looking to find a simple tool to take an XSD file and convert the XSD tree structure to another text format (e.g. an...
20
by: Mr.SpOOn | last post by:
Hi, I need a structure to represent a set of integers. I also need to perform on this set some basic set operations, such as adding or removing elements, joining with other sets and checking for...
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: 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...
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
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
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...
0
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...
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.