46

I'm starting a Python project and expect to have 20 or more classes in it. As is good practice I want to put them in a separate file each. However, the project directory quickly becomes swamped with files (or will when I do this).

If I put a file to import in a folder I can no longer import it. How do I import a file from another folder and will I need to reference to the class it contains differently now that it's in a folder?

Thanks in advance

Teifion
  • 108,121
  • 75
  • 161
  • 195

4 Answers4

31

Create an __init__.py file in your projects folder, and it will be treated like a module by Python.

Classes in your package directory can then be imported using syntax like:

from package import class
import package.class

Within __init__.py, you may create an __all__ array that defines from package import * behavior:

# name1 and name2 will be available in calling module's namespace 
# when using "from package import *" syntax
__all__ = ['name1', 'name2'] 

And here is way more information than you even want to know about packages in Python

Generally speaking, a good way to learn about how to organize a lot of code is to pick a popular Python package and see how they did it. I'd check out Django and Twisted, for starters.

Rodrigo
  • 5
  • 2
Kenan Banks
  • 207,056
  • 34
  • 155
  • 173
22

"As is good practice I want to put them in a separate file each. "

This is not actually a very good practice. You should design modules that contain closely-related classes.

As a practical matter, no class actually stands completely alone. Generally classes come in clusters or groups that are logically related.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • 3
    I've always felt that one-class-per-file was a misfeature of the Java language. A well-intentioned one, perhaps, but still. – Ben Blank Dec 24 '08 at 18:12
  • 2
    @Ben Blank: One public class per file is what you do when you've got a very complex language and a very slow compiler. If you simplify your language, you get faster compilers and you don't need that administrative hack. – S.Lott Dec 24 '08 at 19:02
12

Python doesn't force you into Java's nasty one-class-per-file style. In fact, it's not even considered good style to put each class in a separate file unless they are huge. (If they are huge, you probably have to do refactoring anyway.) Instead, you should group similar classes and functions in modules. For example, if you are writing a GUI calculator, your package layout might look like this:

/amazingcalc
   /__init__.py # This makes it a Python package and importable.
   /evaluate.py # Contains the code to actually do calculations.
   /main.py # Starts the application
   /ui.py # Contains the code to make a pretty interface
Benjamin Peterson
  • 19,297
  • 6
  • 32
  • 39
  • They'll probably end up fairly large – Teifion Dec 24 '08 at 17:43
  • 2
    @Teifion: Large? If a class is "large" (i.e., many hundreds of lines of code), it's probably doing too much. Good practice is to decompose such a large class into something easier to understand. Unrelated to using many files. – S.Lott Dec 24 '08 at 22:28
6

simple answer is to create an empty file called __init__.py in the new folder you made. Then in your top level .py file include with something like:

import mynewsubfolder.mynewclass
Ali Afshar
  • 40,967
  • 12
  • 95
  • 109
fuentesjr
  • 50,920
  • 27
  • 77
  • 81