0

I have Workflow.py file containing following function:

class Workflow:
    def Validation(self):
        return print ("Validation Process Started..")

I have another file Cron.py.

import schedule
import time
from Workflow import Validation

class Cron:

    def my_job(self):
        print('Foo')
        Workflow.Validation()

    def Start(self):
        schedule.every(2).to(4).seconds.do(self.my_job)
        while 1:
            schedule.run_pending()
            time.sleep(1)

A = Cron()
A.Start()

I am trying to call Validation() function present in Workflow.py from Cron.py file. Both of these files are places in same folder. But, the code is giving following exception:

Traceback (most recent call last):
 File "cron.py", line 3, in <module>
 from Workflow import Validation
 ImportError: cannot import name 'Validation'

Please can anyone help on resolving this error.

2
  • 1
    I think you are looking for from Workflow import Workflow which should fix it Commented Nov 22, 2019 at 21:55
  • 1
    Validation is not a top level function. It's a class method. Change your import to from Workflow import Workflow Commented Nov 22, 2019 at 21:55

2 Answers 2

1

You have a Workflow class inside of a Workflow file which is why you're confused. There is no Validation to import, because Validation is inside of the class (and not the file)

The correct way would be:

from Workflow import Workflow

And then you could call it like this:

Workflow.Validation()

However, please note that this function is not a class method or a static method as it is defined right now. If that's how you want to call it, it should look like this:

class Workflow:
    @staticmethod
    def Validation():
        return print ("Validation Process Started..")

In general if your method doesn't use self or cls inside of it, it should be a static method

Sign up to request clarification or add additional context in comments.

1 Comment

plus one for mentioning the class/static method issue
0

this should work

import Workflow
flow = Workflow.Workflow()
flow.Validate()

ps. you should read about static methods, or avoid using classes unecessarily

1 Comment

@OferSadan right, thanks! yeah I admit I answered it without putting through an interpreter :-|

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.