Running a program using os.system in Python - 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.

Related

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:

Running Tensorflow script from another location

I have created Tensorflow image classifier and image classifier should be called using another python script.
I tried os.system() but I cant just call Tensorflow scripts coz it's depends on the multiple files in the Tensorflow script location. so I have to include all the files with classifier script in the main script(2nd python script).
What is the best way to do this?
when script is running :
script error when running from another location :
Do you have tried Python Subprocess instead of os.system?
From How do you use subprocess.check_output() in Python? you can see this simple demo:
py2.py:
import sys
print sys.argv
py3.py:
import subprocess
py2output = subprocess.check_output(['python', 'py2.py', '-i', 'test.txt'])
print('py2 said:', py2output)
Running it:
$ python3 py3.py
py2 said: b"['py2.py', '-i', 'test.txt']\n"
UPDATE:
I think your problem it that you need to specify/set the correct folder path before running your python script. For example:
Consider you have the following folder structure:
/root/project/script.py
/root/project/data/file.txt
If in your python script you load a file using a relative path like ./data/file.txt, you need to run your script inside the project folder.
If you run it in the root folder, like this:
/root$ python project/script.py
the script fails, because it tries to find the data/file.txt inside the root folder.
You can use the cd command to change the current directory before running your python script. You can do it while calling the os.system, like this:
os.system("cd project && python script.py") # for my example case
whereever you call your python script from, all relative paths in your script will be based on. i.e. you called the script from its project folder in the first image, so the links to project_folder/tf_files/... were working, whereas you lateron called it from elsewhere, so that the symbolic links were messed up. your script tried to find elsewhere/tf_files/... but that subfolder does not exist.
you could edit the script so all paths are always abolute (starting with /home/...), then there is no way of confusing the

How to run a python function from a file using python commandline?

I am using python.exe.
I tried:
C:/myfile.py
python C:/myfile.py
python "C:/myfile.py"
It always says "invalid syntax". The code is this one:
https://github.com/paulnasca/paulstretch_python/blob/master/paulstretch_stereo.py#L150
So not sure if the file has bugs or I am doing something wrong.
Your screenshot shows that you are already in the Python interpreter. Trying to run python again will result in an error. Exit the interpreter by hitting CtrlD. Make sure you have downloaded the complete paulstretch_stereo.py file. Put the file in the same directory as the files you want to process. Then, from the Windows command line, run python paulstretch_stereo.py --help and the program's options should print out.
By the way, make sure you have NumPy and SciPy installed, otherwise the program won't run.
What you get when you run python.exe directly is called the interactive interpreter.
The usual way to run a python module is simply providing it as a command-line option to the python process:
python C:/myfile.py
This command is provided from your command-line, not from the interactive interpreter.

Getting Python error "from: can't read /var/mail/Bio"

I am running a (bio)python script which results in the following error:
from: can't read /var/mail/Bio
seeing as my script doesn't have anything to with mail, I don't understand why my script is looking in /var/mail.
What seems to be the problem here? i doubt it will help as the script doesn't seem to be the problem, but here's my script anyway:
from Bio import SeqIO
from Bio.SeqUtils import ProtParam
handle = open("examplefasta.fasta")
for record in SeqIO.parse(handle, "fasta"):
seq = str(record.seq)
X = ProtParam.ProteinAnalysis(seq)
print X.count_amino_acids()
print X.get_amino_acids_percent()
print X.molecular_weight()
print X.aromaticity()
print X.instability_index()
print X.flexibility()
print X.isoelectric_point()
print X.secondary_structure_fraction()
what is the problem here? bad python setup? I really don't think it's the script.
No, it's not the script, it's the fact that your script is not executed by Python at all. If your script is stored in a file named script.py, you have to execute it as python script.py, otherwise the default shell will execute it and it will bail out at the from keyword. (Incidentally, from is the name of a command line utility which prints names of those who have sent mail to the given username, so that's why it tries to access the mailboxes).
Another possibility is to add the following line to the top of the script:
#!/usr/bin/env python
This will instruct your shell to execute the script via python instead of trying to interpret it on its own.
I ran into a similar error when trying to run a command.
After reading the answer by Tamás,
I realized I was not trying this command in Python but in the shell (this can happen to those new to Linux).
Solution was to first enter in the Python shell with the command python
and when you get these >>>
then run any Python commands.
Same here. I had this error when running an import command from terminal without activating python3 shell through manage.py in a django project (yes, I am a newbie yet). As one must expect, activating shell allowed the command to be interpreted correctly.
./manage.py shell
and only then
>>> from django.contrib.sites.models import Site
Put this at the top of your .py file (for Python 2.x)
#!/usr/bin/env python
or for Python 3.x
#!/usr/bin/env python3
This should look up the Python environment. Without it, it will execute the code as if it were not Python code, but shell code. If you need to specify a manual location of the Python environment, put
#!/#path/#to/#python
for Mac OS just go to applications and just run these Scripts Install Certificates.command and Update Shell Profile.command, now it will work.
For Flask users, before writing the commands, first make sure you enter the Flask shell using:
flask shell

importing the wx module in python

When I import the wx module in a python interpreter it works as expect. However, when I run a script (ie. test.py) with wx in the imports list, I need to write "python test.py" in order to run the script. If I try to execute "test.py" I get an import error saying there is no module named "wx". Why do I need to include the word python in my command?
PS the most helpful answer I found was "The Python used for the REPL is not the same as the Python the script is being run in. Print sys.executable to verify." but I don't understand what that means.
Write a two line script (named showexe.py for example):
import sys
print sys.executable
Run it both ways as showexe.py and python showexe.py. It will tell you if you're using the same executable in both cases. If not, then it'll depend on your operating system what you have to do to make the two run the same thing.
If you start your script with something like #!/usr/local/bin/python (but using the path to your python interpreter) you can run it without including python in your command, like a bash script.

Categories

Resources