3

I have a python file with name Pqr.py which contains a class holding a static method.

import subprocess

class Pqr:

    @staticmethod
    def callTheService(a,b,c):
       subprocess.call(a,b,c)

Now I am trying to access this static method from another class which is in other python file. Both .py files are located in same directory. The code in second file is,

import Pqr

class Rst:
    Pqr.callTheService("a", "b", "c")

When I try to run this, I get an error of AttributeError: module 'Pqr' has no attribute 'callTheService'

Could you please help me solve this error?

4
  • 1
    is your file named Pqr.py? You need to access the class not the module, so use Pqr.Pqr.callTheService. In python, you do not generally give the module the same name as a class (Python != Java). BTW, this would have been more obvious if you follow Python naming conventions. Generally, you use lower_case for module names, and avoid camelCase at all costs! Commented Dec 21, 2017 at 16:08
  • Here is the link to the Style Guide for Python Code. Commented Dec 21, 2017 at 16:11
  • Wow, @juanpa.arrivillaga you solved my problem within minutes! Thanks a ton. Commented Dec 21, 2017 at 16:13
  • @Hannu I mean, it could be a static method, why should it be a class method? More likely, it should just be a module level function. So using a class here makes no sense at all, but I'm assuming that Pqr should be a class to begin with, and this is a toy example. Commented Dec 21, 2017 at 16:14

1 Answer 1

1

I solved the problem reading comments. I imported the class within the module. Here is the sample working code.

from Pqr import Pqr

class Rst:
    Pqr.callTheService("a", "b", "c")
Sign up to request clarification or add additional context in comments.

2 Comments

You should probably rename your module. I'm not sure what you mean "I imported the file instead of the entire module", but it doesn't sound correct.
@juanpa.arrivillaga sorry I am new to Python, right now going through the documentation you shared with me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.