0

I have a quick question with regards to making a module out of a python script.

Let's say I want to call my module: testingA

Currently my directory structure is like so:

  • app
  • config.py
  • run.py

Question

  1. Can I still make a module called testingA with that directory structure, or would I have to rename the "app" directory to "testingA"
  2. Is it pythonic to have the main application folder match the module name?

Any input would be appreciated.

J.Zil
  • 2,397
  • 7
  • 44
  • 78

1 Answers1

1

If you have a directory lets call it d. and it looks like this:

d/app/
d/config.py
d/run.py

And you want to add a new script called testingA.py so that it will be:

d/app/
d/config.py
d/run.py
d/testingA.py

That would be fine.

You do not need to rename d to anything. And if you want to import testingA.py as a module from run.py for instance, you would just do:

import testingA

Or you could do:

from testingA import *

If you want to put testingA.py into d/app/ however, like so:

d/app/testingA.py
d/config.py
d/run.py

Then you will need to do something like this in run.py:

import app.testingA

Or:

from app import testingA
Inbar Rose
  • 41,843
  • 24
  • 85
  • 131
  • What about if I wanted my testingA module to be the entire contents of /app? Would I need to make /app/ into /testingA/? – J.Zil Nov 11 '13 at 12:08
  • I don't think you understand what a "module" means. Try reading [the docs](http://docs.python.org/2/tutorial/modules.html), [this SO question](http://stackoverflow.com/questions/9383014/cant-import-my-own-modules-in-python), and [this](http://www.ibiblio.org/g2swap/byteofpython/read/making-modules.html) – Inbar Rose Nov 11 '13 at 12:13
  • Damn, I think I meant "source distribution" – J.Zil Nov 11 '13 at 12:20
  • Using your new found knowledge, [ask another question.](http://stackoverflow.com/questions/ask) – Inbar Rose Nov 11 '13 at 12:44
  • In this case the answer is yes. You have to rename `app` to `testingA`. – Torsten Engelbrecht Nov 11 '13 at 12:46
  • @Torsten I think you mean he has to rename `d` to `testingA` since `app` is inside the directory already where he wants his distribution to be. – Inbar Rose Nov 11 '13 at 12:48
  • If `d` is the project directory (but not a package itself) and he wants to do `from testingA import whatever` then no, I meant the `app` directory. `testingA` wouldn't be a python module of course, but a python package instead. – Torsten Engelbrecht Nov 11 '13 at 13:32