Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a program from another in python

Tags:

python

call

I'm new to python programming and I have a problem. I've been looking for the solution to it problem all day and nothing I've found so far has helped me. I'm writing a time delay program in Python, but once it hits the input for the delay it gives me an error. I've tried running it in the same program and it works, but I want the two programs to be separate.

This is the delay function in delay.py

def delayA(ina):
    ina=float(ina)
    print("okay!")
    time.sleep(ina)
    print("done!")

This is the call for it in my main

import delay.py

ina = input("Enter delay in seconds: ")
delayA(ina)

And this is the error message that I've been getting all day

Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 2218, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/Python/inputcall.py", line 1, in <module>
    import delay.py
ImportError: No module named 'delay.py'; 'delay' is not a package

Thank you in advance for any help!

like image 791
SassyMouthSalmon Avatar asked Jun 15 '26 03:06

SassyMouthSalmon


1 Answers

You were almost there bar a few minor mistakes:

delay.py:

from time import sleep


def delayA(ina):
    ina = float(ina)
    print("okay!")
    sleep(ina)
    print("done!")

main.py:

#!/usr/bin/env python

from delay import delayA


ina = input("Enter delay in seconds: ")
delayA(ina)

Your only three mistakes I found were:

  • Lack of indentation in your delayA function.
  • from delay import delayA -- Not: import delay.py
  • Actually importing the delayA function. i.e: from foo import bar
like image 137
James Mills Avatar answered Jun 16 '26 20:06

James Mills