tags:

views:

281

answers:

2

I have a file that I want to include in Python but the included file is fairly long and it'd be much neater to be able to split them into several files but then I have to use several include statements.

Is there some way to group together several files and include all at once?

+6  A: 

Yes, take a look at the "6.4 Packages" section in http://docs.python.org/tut/node8.html:

Basically, you can place a bunch of files into a directory and add an __init__.py file to the directory. If the directory is in your PYTHONPATH or sys.path, you can do "import directoryname" to import everything in the directory or "import directoryname.some_file_in_directory" to import a specific file that is in the directory.

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. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.

Readonly
there should be two underscores before and after "init" and "all". I can't seem to get them to display properly :(
Readonly
+8  A: 
  1. Put files in one folder.
  2. Add __init__.py file to the folder. Do necessary imports in __init__.py
  3. Replace multiple imports by one:

    import folder_name

See Python Package Management

J.F. Sebastian