1

I have been working with Python for about a year now, and I'm starting to write my own modules. I'm trying to make a module that imports a bunch of useful modules at the same time. The module imports fine. When I try to run it, it gives the error

Traceback (most recent call last):
    File "<pyshell#1>", line 1, in <module>
        importuseful()
TypeError: 'module' object not callable

My file name is importuseful.py My code is:

def importuseful():

    import sys

    import os

    import RPi.GPIO as GPIO

    import GPIO as gpio

    import random

    import math

    import time

    import email

    import tty

    import turtle

I run at the IDLE shell:

from Modules import importuseful

importuseful()

Here is where it throws the TypeError

The path it is in is /home/pi/Modules, and that is set as part of $PYTHONPATH.

The only other module I have made is GPIO which is one I am trying to import. I was having the same problem with that one, but that one fixed itself. Anyone have any ideas on what is wrong or how to fix?

Ryan
  • 13
  • 7
  • 2
    possible duplicate of [TypeError: 'module' object is not callable](http://stackoverflow.com/questions/4534438/typeerror-module-object-is-not-callable) – Cyphase Aug 18 '15 at 00:01
  • could you also give the code of how you wrote the module. – patapouf_ai Aug 18 '15 at 00:01
  • You should search before asking a question; the duplicate is showing up in the 'Related' list :). – Cyphase Aug 18 '15 at 00:02
  • @Cyphase, I read that one before posting, and I tried what it said, and it didn't work. I now realize why it didn't work. – Ryan Aug 18 '15 at 00:07

2 Answers2

0

You need to access the function using the module name, you have imported the module so importuseful() is trying to call the module not the function defined in your module:

 importuseful.importuseful()

Or if you wanted to import the function from the module, you would use:

 from importuseful import importuseful
 importuseful()

Or using your Modules:

 from Modules.importuseful import importuseful 

If you are trying to actually import all the modules using the script. forget the function and just do the imports.

import sys
import os
import RPi.GPIO as GPIO
import GPIO as gpio
import random
import math
import time
import email
import tty
import turtle

Then if you want to be able to use all the modules just use importuseful.math etc... You can use from from Modules.importuseful import * but that is never rarely a good idea, what you can so is shorten the name from Modules.importuseful import importuseful as im the im.math(.. etc..

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • @Ryan, yes, you have to use the module name to access anything defined in it unless you actually import the functions from the module as in the second example, you could also `from Modules.importuseful import importuseful ` – Padraic Cunningham Aug 17 '15 at 23:58
  • It seems like it did not import the wanted modules. It ran the module with no error, but did not import them. – Ryan Aug 18 '15 at 00:01
  • If it did not error then it did import, are you trying to actually access what is inside the function or use the function? – Padraic Cunningham Aug 18 '15 at 00:01
0

As Padraic Cunningham said, you need to call the function inside the module.

from Modules import importuseful

importuseful.importuseful()

However, this isn't going to do what you want, because the modules will only be imported in the function scope. For instance:

>>> def importuseful():
...     import math
... 
>>> importuseful()
>>> math
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'math' is not defined
>>> 

You should just include only the modules you need at the top of every file. No need to import unnecessary modules.

If you really want to do this (which I don't recommend), you could put the imports directly into a module, then import everything from that module.

# usefulimports.py

import functools
import itertools
import math

# script.py

from usefulimports import *
Cyphase
  • 11,502
  • 2
  • 31
  • 32
  • Ok. I see how it works now. Thanks for explaining it. – Ryan Aug 18 '15 at 00:03
  • @Ryan, I added a way to do what you want, though I still don't recommend it. – Cyphase Aug 18 '15 at 00:06
  • Thanks, I may modify the functions it imports to ones I know I will need in programs, so there are no unnecessary ones. – Ryan Aug 18 '15 at 00:12
  • @Ryan, why not just have the imports directly in the file that's using them? Why separate them out into this other file? – Cyphase Aug 18 '15 at 00:13
  • When working with the GPIO pins on my Raspberry Pi, I always use RPi.GPIO, GPIO, time, and math. I think it is faster to import everything from an external file, than import them all by hand every time. – Ryan Aug 18 '15 at 00:17
  • Faster? By hand? I assume you mean in the REPL, not in a script. If so, I see where you're coming from in doing this. I'd just recommend not doing it in a script, where you just have to type it once :). – Cyphase Aug 18 '15 at 00:23