Calling a program from another in python - python

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!

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

Related

Playsound Module in python

I am not able to run a audio file automatically when the program started i saw playsound module but it is giving me error
Traceback (most recent call last):
File "main.py", line 7, in <module>
playsound('Music/videoplayback(2).mp3')
TypeError: 'module' object is not callable
my code
import random
import playsound
#Creating a KBC Special Quiz!
print("'\033[1m'"+"Welcome To Kaun Banega Crorepati!")
print()
print()
playsound('Music/videoplayback(2).mp3')
i am using repl, even i am finding difficulties in pathing
You are using the playsound module instead of the playsound.playsound function.
The docs show you how to import it so it works like you expected, see below.
>>> from playsound import playsound
>>> playsound('/path/to/a/sound/file/you/want/to/play.mp3')

windows-curses not working with an import error

I'm just doing some testing with windows-curses (2.3.0) and I was using this code and it gave me an error. The code and error are below.
Code:
import curses
from curses import wrapper
def main(stdscr):
stdscr.clear()
stdscr.addstr("Hello World")
stdscr.refresh()
stdscr.getch()
wrapper(main)
Error:
Traceback (most recent call last):
File "C:\Users\norbe\OneDrive\Desktop\Projects\Testing\Curses\curses.py", line 1, in <module>
import curses
File "C:\Users\norbe\OneDrive\Desktop\Projects\Testing\Curses\curses.py", line 2, in <module>
from curses import wrapper
ImportError: cannot import name 'wrapper' from partially initialized module 'curses' (most likely due to a circular import)
The code already imported curses and its context (methods and variables) binded to it.
It means that wrapper is already identified by python.
Try this :
import curses
wrapper = curses.wrapper

It is a simple source code, but it does not run

import tailer
test = tailer.tail(open("test.txt"), 1)
#print(lines[1])
It's as simple as the code above, but it doesn't work.
(I saved it because it was successful once during the experiment, but an error occurs when I run it again later.)
Error content:
Traceback (most recent call last):
File "c:\Users\user\Documents\VSCODE\python\V1\tailer.py", line 1, in <module>
import tailer
File "c:\Users\user\Documents\VSCODE\python\V1\tailer.py", line 3, in <module>
test = tailer.tail(open("test.txt"), 1)
AttributeError: partially initialized module 'tailer' has no attribute 'tail' (most likely due to a circular import)
Looks like your file is called tailer.py, so when it does import tailer, it tries to load itself, which is usually a recipe for confusion.
You named your program tailer.py. When you do an import tailer the local folder has priority over all other folders and you will import tailer.py again. Creating an import circle.
In other words: you have a name clash between your program and the library you are trying to import. Just rename the file to something else and try again.

Using pdb, how can I run a program and pause where reaching an error?

Using python interpreter and/or pdb, can we run a program and pause whenever reaching an error, so that I can examine all the frames of the call stack of the program at the time of crashing?
When I run a program directly inside python interpreter, when reaching an error, it tells where the line of code it happens, but it seems return to the topmost frame, and I can't examine the frame where the error actually happens. E.g.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test.py", line 194, in <module>
addlevel(root_toc, 0)
File "test.py", line 191, in addlevel
addlevel(child, root_level+1)
File "test.py", line 188, in addlevel
root.value.append(root_level)
AttributeError: 'str' object has no attribute 'append'
>>> root_level
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'root_level' is not defined
The error happens at the lowest frame, and I can't examine the value of root_level at that frame. Is it because it returns to the topmost frame after the error happens? How can examine the lowest frame?
THanks.
Run pdb as a module, passing the script you want to debug. It will break on abnormal exits. (This is mentioned early in the docs.)
python -m pdb my_script.py
If you're in the interpreter, you can use pdb.pm() to debug the last traceback.
Or, use the IPython interpreter. Typing debug after an uncaught exception will enter a pdb session for the last traceback, similar to pm().

Import * include submodules

I have a directory structure that looks like this:
scripts/
__init__.py
filepaths.py
Run.py
domains/
__init__.py
topspin.py
tiles.py
hanoi.py
grid.py
I would like to say:
from scripts import *
and get the stuff that is in filepaths.py but also get the things that are in hanoi.py
The outer __init__.py contains:
__all__ = ['filepaths','Run','domains','hanoi']
I can't figure out how to get the inner files to be included in that list. Putting hanoi by itself gets this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'hanoi'
Putting domains.hanoi gets this error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'domains.hanoi'
The last reasonable guess I could come up with is putting scripts.domains.hanoi which gets this error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'scripts.domains.hanoi'
How do you get the all list to include things that are in subdirectories?
In scripts/__init__.py, before the __all__ add the following
from domains import topspin, tiles, hanoi, grid
This will add those modules to the namespace, and you will be able to import them with
from scripts import *
Note
As a soapbox, it is preferred to do things like
from scripts import topspin, tiles, hanoi, grid, filepaths, Run
over
from scripts import *
because 6 months from now, you might look at hanoi on the 400th line of code and wonder where it came from if you use the * import style. By explicitly showing what is imported from scripts it serves as a reminder where things come from. I'm sure that anyone trying to read your code in the future will thank you.
Import them first, in the __init__ files.
In scripts/__init__.py, import at least domains, and in scripts/domains/__init__.py import hanoi, etc. Or import domains.hanoi directly in scripts/__init__.py.
Without importing these, the scripts/__init__.py module has no reference to the nestend packages.

Categories

Resources