1

I am learning python and coming from a java/kotlin background i am tryin to figure out how to use a class defined in another file invoke the functions inside it?

Here is my class:

class Practise:
    def messageFunction(message):
        print("your message you entered is " + message)
        return

    def messageFunction2(message):
        print("mesage 2 " + message)
        return

    def messageFunction3(message):
        print("message 3  " + message)
        return

Here is the main python file i want to invoke the functions inside the class above(that is on a seperate file)

from practise import Practise

practise = practise.Practise()

practise.messageFunction("wagwan")

I get this error:

File "/home/me/Work/PycharmProjects/practise/venv/Main.py", line 3, in <module>
    practise = Practise()
TypeError: 'module' object is not callable`
Jono
  • 17,341
  • 48
  • 135
  • 217
  • 1
    Is the file that contains `Practise` called `Practise`? If that's the case, then try: `from Practise import Practise`. – Hampus Larsson Jun 12 '20 at 15:53
  • 1
    https://stackoverflow.com/questions/41276067/importing-class-from-another-file Does this help? – burntchowmein Jun 12 '20 at 15:54
  • As an aside - in Python classes are typically used to create instances of an object with different state. Although classes can be used to group together functions it is more typical to just define those functions in a module and let the module be the *grouping* mechanism. – wwii Jun 12 '20 at 16:02
  • Does this answer your question? [Using class/function in another module](https://stackoverflow.com/questions/27995900/using-class-function-in-another-module) – wwii Jun 12 '20 at 16:04
  • Cannot reproduce in Python version 3.8. `from Temp import Temp; t = Temp()` --> `` – wwii Jun 12 '20 at 16:10
  • 1
    Could you please share the file structure showing the names of the folder and files? – Craig Jun 12 '20 at 16:11

1 Answers1

2

The issue is with the way you're importing the other module. You should do something like the following

import <modulename>

practise = <modulename>.Practise()

practise.messageFunction("wagwan")

Or

from <modulename> import Practise

practise = Practise()

practise.messageFunction("wagwan")

where <modulename> is the name of the file you define the class over.

Tibebes. M
  • 6,940
  • 5
  • 15
  • 36
  • That did not work. still getting the same error. the Practise class is inside Practise.py – Jono Jun 12 '20 at 16:03
  • use [snake_case](https://namingconvention.org/python/module-naming.html) for module names (file name). That should resolve the conflict – Tibebes. M Jun 12 '20 at 16:06
  • so in this case, rename your file `practise.py` and `from practise import Practise` – Tibebes. M Jun 12 '20 at 16:08
  • nope that did not work, same error. renamed both files to lowercase. – Jono Jun 12 '20 at 16:12
  • from practise import Practise practise = practise.Practise() practise.messageFunction("wagwan") – Jono Jun 12 '20 at 16:14
  • since you're using the `from .. ` syntax, let the second line be "`practise = Practise()`" (without "`practice.`") – Tibebes. M Jun 12 '20 at 16:15
  • Am getting this error now File "/home/jonathanrichards/Work/PycharmProjects/practise/venv/main.py", line 5, in practise.messageFunction("wagwan") TypeError: messageFunction() takes 1 positional argument but 2 were given even though i supplied one arg only? – Jono Jun 12 '20 at 16:17
  • Oh the import has worked okay now.. this is another issue. to solve it, add "`self`" as first parameter to the methods inside the class. like this `messageFunction3(self, message)` – Tibebes. M Jun 12 '20 at 16:19