I am using am using Python 2.7 on MacOS and want to use a bash command within a python script.
command = "someProgram --option1 value 1 --option2 value 2"
I had to include the path of this program in my bash_profile in order to run it. I tested so far:
os.system(command)
and
subprocess.check_call(command.split(" "),shell=True)
Neither worked. The latter threw error 127 and the first one only returned 32512. A google search told me that this occurs when the command is not known.
If I now start this command within the terminal everything works perfectly fine.
Do I have to include something such that python can find this command? Why is this behavior?
With shell=True the cmd needs to be a string.
subprocess.check_call(command, shell=True)
where command is of type str
Thanks for your help. The final solution is kind of stupid. I started spyder via the anaconda GUI. If I do so the above code does not work.
If I run this directly via the console or start spyder via the console everything is fine. It seems that the bash_profile is not loaded when spyder is loaded but requires the console to do so
Related
I would like to include a command to create a 7zip archive withinin a Python script. Since I am working on Windows, I need to pass the command to the powershell console. I am planning to do it with os.system (I am aware that this is not the best way to do it and that I should use subprocess, but I really just need a quick fix and it would not be time effective for me to learn to use a new module in this context).
The following command works if run from the powershell console
&'C:\\Program Files\\7-Zip\\7z' a -mx=0 X:/myarch.zip X:/myarch
So I recreate the same string within python like this:
cmdl = r"&'C:\\Program Files\\7-Zip\\7z' a -mx=0 X:/myarch.zip X:/myarch"
The string is interpreted as follow:
"&'C:\\\\Program Files\\\\7-Zip\\\\7z' a -mx=0 X:/myarch.zip X:/myarch"
Now, if I copy-paste the above string within the powershell console, it runs without problems. However, if I run it within python using os.system(cmdl) I got the following error
"The filename, directory name, or volume label syntax is incorrect"
Why is this the case and how can I fix this issue ?
os.system is meant for executing cmd commands, cmd commands can be ran in powershell maybe after all powershell is a bit advanced but I'm sure that you can't run a cmd command in powershell, henceforth your code is not working.
However a creative solution for executing a powershell command from python(not using python) would be to write your command into a .ps file(powershell script)and then run it using os.startfile()(use this code: os.startfile("script.ps"))
I have a python program which prints long outputs. When i try to run that file in vscode, its interactive window isn't enough to view full output. So is there any way to run python file in cmd from VSCODE?
If you are running windows, VSCode uses Powershell as your terminal by default. If you want to use the command prompt instead, hit ctrl+shift+p, type Shell into the command pallet, select Terminal: Select Default Shell, and change it to Command Prompt. I am not sure this will fix your problem as I think Powershell should display just as much output as the CMD, but if you want to try switching terminals, that will do it. Another option is to try to run it natively in CMD or Powershell, rather than using the VSCode integrated terminal. That might be better if changing terminals doesn't help.
As #Jeremiah said, you can also just run your script with the Cmd prompt, without using vs code. Let's say you have the file 'test1.py' saved as C:\Users\bcubrich\Documents\test1.py, and your python env .exe is saved in C:\python27\ArcGIS10.5\python.exe. I just wrote script that had this in it:
print('worked')
Then just input this into the Cmd prompt
C:\python27\ArcGIS10.5\python.exe C:\Users\bcubrich\Documents\test1.py
And it printed
worked
to the console.
More on running python through cmd console here:
http://www.cs.bu.edu/courses/cs108/guides/runpython.html
I have a Raspberry Pi and I'm attempting to create a Python script. However the issue is that I'm unable to run the script from the Terminal, it throws syntax errors yet the same code works just fine in the Python 3.5.3 Shell.
I'm trying the simplest thing such as a printand I've tried various ways with the parentheses and quotation marks, yet no luck with executing the script in the terminal.
I'll include a simple Imgur link of a screenshot, showing how the code is successfully executed in the Shell but not in the terminal.
https://imgur.com/a/lLSnq
The code:
print ("test")
Any assistance is greatly appreciated in advance!
The error was that your terminal didn't know that the code you tried to execute was python, therefore it tried to execute it with the bash interpreter.
Adding the correct shebang to specify the use of the python interpreter fixed the problem.
#!/usr/bin/env python
print("test")
you can execute python scripts, i.e. the script boa.py from terminal by python boa.py
I am new to python and wanted to make a simple script that acted like the ls command in a mac/linux terminal but for cmd in windows. The code itself works and if I run the script using python ls_script.py in my cmd it works fine. However, I want to make it so that I can run it in any active directory by just typing in ls in my cmd. I made an environment variable in cmd called ls that has a value of python ....\ls_script.py, which assumed would work since if i were to type that exact thing in manually, it works. However, when I just type in ls, it gives the following error:
"'ls' is not recognized as an internal or external command, operable program, or batch file."
I don't think your problem has anything to do with python, considering that the python script does what you want. The problem is getting the environment variable to work, right?
I believe this question has the answer you're looking for:
How to create ls in windows command prompt?
In short, it looks to me like the way to achieve what you wanted was to not use environment variables, but to create a batch file instead.
I'm trying to learn python but have some problem running source files from power shell. When I type 'python' it opens up and I can type python commands directly in the shell. I think this is called interactive mode. But when I try to run/execute a source file I get an error message: It sayys: Syntax error: invalid syntax.
I use 'python myfile.py' when I try to execute the script.
If I run the same file from IDLE it works just fine. Ana idea what I'm doing wrong?
Here is myfile.py. I'm running python 2.7
# filename: myfile.py
while True:
s = raw_input('Enter something: ')
if s == 'Quit':
break
print 'Lenght of the string is', len(s)
print 'Done'
You might have more than one version of Python installed and the version IDLE is using is newer. To see what version of python you have you can type >python -V at a command line. If that version looks appropriate then you might need the full path to the file as the second parameter. E.g >python C:\myfile.py.
If you installed Python correctly there is always a chance that just typing the name of the script will run it with python. E.g. >myfile.py
I always find that adding C:\Python27 to the %PATH% variable and .PY to the %PATHEXT% variable makes running scripts easier. In this case just >myfile should work.
Edit after Update:
Typing just >python with no parameters opens python in 'interactive mode' which is different from the batch or scripting mode that your script is intended for. If executed with arguments the first argument is taken as the file path and further arguments are passed to the script in the sys.argv list.
You will need to put the full path of the Python executable within the command line in order for it to work. You could check and ensure that your python exe is included in your Path among your system variables.
Disclaimer: I don't know PowerShell, but I do know cmd.exe.
I don't know why python myfile.py doesn't work, but assuming that PowerShell bears at least some similarity to cmd.exe, the following should probably work: myfile.py. That's right, just enter the name of the Python script and hit enter.
If you started by typing "python" in powershell you will need to get out of that script.
If you are in python type:
quit()
then type
python myfile.py
This should work if your python is installed correctly.
Try to type this in Powershell:
$env:path="$env:Path;C:\Python33
After this, command
python yourfile.py
should work.
This my sound silly, especially coming from a beginner.
Just save the file on your desktop. Open up powershell and drag the file directly into powershell and it opens. kind of tedious but it works