3

I'm teaching myself Python (I have experience in other languages).

I found a way to import a "module". In PHP, this would just be named an include file. But I guess Python names it a module. I'm looking for a simple, best-practices approach. I can get fancy later. But right now, I'm trying to keep it simple while not developing bad habits. Here is what I did:

  1. I created a blank file named __init__.py, which I stored in Documents (the folder on the Mac)

  2. I created a file named myModuleFile.py, which I stored in Documents

  3. In myModuleFile.py, I created a function:

    def myFunction()
            print("hello world")
    
  4. I created another file: myMainFile.py, which I stored in Documents

  5. In this file, I typed the following:

    import myModuleFile.py
    
    myModuleFile.myFunction()
    

This successfully printed out "hello world" to the console when I ran it on the terminal.

Is this a best-practices way to do this for my simple current workflow?

I'm not sure the dot notation means I'm onto something good or something bad. It throws an error if I try to use myFunction() instead of myModuleFile.myFunction(). I kind of think it would be good. If there were a second imported module, it would know to call myFunction() from myModuleFile rather than the other one. So the dot notation makes everybody know exactly which file you are trying to call the function from.

I think there is some advanced stuff using sys or some sort of exotic configuration stuff. But I'm hoping my simple little way of doing things is ok for now.

Thanks for any clarification on this.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
code beginner
  • 285
  • 3
  • 14
  • the proper name for package init file is `__init__.py` – m.wasowski Mar 22 '14 at 08:19
  • possible duplicate of [Python: importing a sub‑package or sub‑module](http://stackoverflow.com/questions/12229580/python-importing-a-subpackage-or-submodule) – m.wasowski Mar 22 '14 at 08:19
  • Some advices: Create a folder for your project, don't use "Documents", name your modules like "my_module.py" don't use CamelCase, It is not common in python – Roberto Mar 22 '14 at 08:22

2 Answers2

3

For your import you don't need the ".py" extension

You can use:

import myModuleFile
myModuleFile.myFunction()

Or

from myModuleFile import myFunction
myFunction()

Last syntax is common if you import several functions or globals of your module.

Besides to use the "main" function, I'd put this on your module:

from myModuleFile import myFunction

if __name__ == '__main__':
    myFunction()

Otherwise the main code could be executed in imports or other cases.

I'd use just one module for myModuleFile.py and myMainFile.py, using the previous pattern let you know if your module is called from command line or as import.

Lastly, I'd change the name of your files to avoid the CamelCase, that is, I'd replace myModuleFile.py by my_module.py. Python loves the lowercase ;-)

Roberto
  • 8,586
  • 3
  • 42
  • 53
  • It is a bad practice to put a directory with `__init__.py` into `sys.path` (usually its *parent* is in `sys.path`). It makes the same module available under different names (multiple module object are possible). `__init__.py` should be removed in this case. – jfs Mar 24 '14 at 07:08
  • Hi @J.F.Sebastian, I agree with you, but who is putting the dir with `__init__.py` into the `sys.path` ? Anyway I'll remove my comment about the `__init__.py` if it's confusing – Roberto Mar 24 '14 at 09:30
  • My comment is about yours *"`__init__.py` is not strictly necessary in your context"* while your code demonstrates `import myModuleFile`. In that context it is harmful to have `__init__.py`. If there is `__init__.py` then you should use `from parent import myModuleFile` instead. – jfs Mar 24 '14 at 09:35
  • Ok, that's right, I removed my comment about `__init__.py`. Thanks. – Roberto Mar 24 '14 at 09:38
2

You only need to have init.py if you are creating a package (a package in a simple sense is a subdirectory which has one or more modules in it, but I think it may be more complex than you need right now).

If you have just one folder which has MyModule.py and MyMainFile.py - you don't need the init.py.

In MyMainFile.py you can write :

import myModuleFile

and then use

myModuleFile.MyFunction()

The reason for including the module name is that you may reuse the same function name in more than one module and you need a way of saying which module your program is using.


Module Aliases

If you want to you can do this :

import myModuleFile as MyM

and then use

MyM.MyFunction()

Here you have created MyM as an alias for myModuleFile, and created less typing.


Here Lies Dragons

You will sometimes see one other forms of IMport, which can be dangerous, especially for the beginner.

 from myModuleFile import MyFunction

if you do this you can use :

 MyFunction()

but this has a problem if you have used the same function name in MyMainFile, or in any other library you have used, as you now can't get to any other definition of the name MyFunction. This is often termed Contaminating the namespace - and should really be avoided unless you are absolutely certain it is safe.

there is a final form which I will show for completeness :

from myModuleFile import *

While you will now be able to access every function defined in myModuleFile without using myModuleFile in front of it, you have also now prevented your MyMainFile from using any function in any library which matches any name defined in myModuleFile. Using this form is generally not considered to be a good idea.

I hope this helps.

Tony Suffolk 66
  • 9,358
  • 3
  • 30
  • 33
  • `Import` is incorrect, use `import` instead. Please, either use the same names as OP (to help the understanding) or follow [pep-8 naming conventions](http://legacy.python.org/dev/peps/pep-0008/#naming-conventions) – jfs Mar 24 '14 at 07:06