how to call Python program - python

I am running a program darknet.py from within my ubuntu terminal by typing python3 darknet.py.
It works perfectly and now I am trying to include it in my own program. But when I call it I get errors. I am not sure what is causing this.
I have already included #!usr/bin/env python3 but no luck.
I am trying to call it with os.systems('/home/bob/DarknetAB/darknet/darknet.py')
The errors I get are as follows
from:cant read /var/mail/cytpes
import -im6.q16: not authorized math#error/constitute.c/writeImage1037
I don't know get why it would work from terminal, but not when I call it from another file?

Did you try to do the following?
import darknet

This is a guess, but it sounds like it could be trying to read your python script like a bash one instead. What about
os.system('python3 /home/bob/DarknetAB/darknet/darknet.py')

Related

Running a program using os.system in Python

I am trying to run Test.py using os.system(path) where I specify the path of the file but I am getting an error. Basically, I want to run Test.py from here and display the output.
import os
os.system(rf"C:\\Users\\USER\\OneDrive-Technion\\Research_Technion\\Python_PNM\\Sept15_2022\\220\\1\\Test.py")
The error is
The system cannot find the path specified.
you are passing a python file, not an executable. You can pass python yourfile.py.
By the way, I would reconsider what you are doing, executing a python script from another python script is quite strange.

cannot obtain output in VS code

i am not getting any output whenever i try to execute any basic command , in python using VS code.
Your Visual Studio Terminal has Python already opened, so when you try to execute the:
& "C:/python 39/python.exe" "C:/route_to_your_file.py"
Is trying to execute a line of code with this, something that has a bad python syntax.
You have two options:
Close python writing exit() and then run your python script with "C:/python 39/python.exe" main2.py or "C:/python 39/python.exe" main3.py
Work directly with the application of python. In that case you can write your code directly on the terminal
print("hello world")
you can (but it is not recommended) import your files as packages, with:
import main2
import main3
Since you are new on python, I recommend you the first option.
You seem to be trying to run python while already in python. Try opening a bash or cmd terminal and typing in python main3.py
Run the code directly:
rather than get into python then run the code:

Can I test a function defined in a file manually in the terminal in vscode?

I have previously only written python code using IDLE but since I am starting to do some more "heavier" programming I figured I should start using Visual Studio Code. I am however having issues doing things that I would like to do while coding to check that my functions are working as intended. The major thing I want to be able to do is if I have saved
def summa(x, y)
return x+y
in a file sum.py, then I would want to test run summa(3, 4) in the terminal.
In IDLE I am used to just running the file containing the function and then use it but I cannot figure out how to do that in Visual Studio Code. However, I realize that it is possible to import the file into a REPL terminal but I would hope that there is some easier way of doing it.
See the answers to this question :
execute python script with function from command line, Linux
You could also use the main function :
if __name__ == '__main__':
summa(sys.argv)
So you just launch the script and it run the function
You can start Python interactively and import your file as a module. Then your function will be available, so you can call it the way you want:
What I did was:
Open new terminal
Type: $ python to start an interactive session
Import module import test or the function from the module: import summa from test
Now I can call summa() and play with it in this manual manner

Executing external Python program in Sikuli script

I am trying to call a python program which just sends me a text on my phone from a Sikuli script.I am a beginner and my goal is to receive a text when a certain loop is finished, but I don't know how to call on it from the Sikuli script. This is a small part of my code from Sikuli so far.
while (something):
if (randomThing):
something = False
subprocess.Popen("python SendSMS.py")
I get an OS error saying no such file or directory. Is there a better way to do this? or what am I doing wrong here? I am using Mac.
subprocess.Popen() will look for SendSMS.py in same directory but if your script is not in that directory you will get find not found error which is totally expected. As a fix you will need to provide whole path to your script instead of just python SendSMS.py replace it with python /home/my_user/SendSMS.py and check if it works.

Executing python code

I am starting fresh with python and trying to execute a code from the python command window. I wrote a file on Desktop\practice\new.py and lunched the python command window.
when I type
C:\users\user\Desktop\practice\new.py
it gives me
SyntaxError: invalid syntax
Executing from CMD worked, but from python window didnt!
Any help?
EDIT2: when i put the compiled code in the directory and use the 'import' it runs, but when the compiled is not in the same directory it won't execute
EDIT: the file contains a simple print statement nd is sytax error free
Everything is explained in here: http://docs.python.org/faq/windows.html#how-do-i-run-a-python-program-under-windows
The main point that when you launch python shell. Its like a live programming. Try to type in it:
>>> print 'hello world'
If you want to launch your file - run in cmd: python C:/users/user/Desktop/practice/new.py
UPDATE: If you do want to run file from within python shell - it was answered here: How to execute a file within the python interpreter?
When you say you're using the "python command window" I'm guessing you mean IDLE...? If so, rather than try to type a command to run a script you've already created as a file, just use File > Open to open that file and then press F5 to run it. Good luck!
The python command window is expecting python commands. Try typing 'import system' or 'print 1+2'.
If you want to run the code in another file you need to use 'import'. Its easier if you start in the same directory, in which case just doing 'import new' will work.
However, there's already a 'new' module in the python library, so the easiest thing to do is to rename your file something else...
It is not working because you are entering the path like c:\users\user\desktop\practice\new.py.....
now try this way: c:/users/user/desktop/practice/new.py
I hope this will work for you i.e. just change '\' to '/'
have a try...
You can run the file like this:
execfile(r'C:\users\user\Desktop\practice\new.py')
Edit: read the comments below this answer before trying it!
Try this:
import sys
sys.path.append("C:\users\user\Desktop\practice\")
import new #won't work - call it something other than new.py...

Categories

Resources