can not execute a python script from windows 7 command line - python-2.7

I am having trouble executing a python script from the command line and I can not seem to figure out why. The Python directory is here (C:\Python27) and I place all the necessary files in that directory. From the command line I can execute:
cd C:\
C:\>C:\Python27\python.exe C:\Python27\run_batch_job.py -h
and verify that everything is installed correctly.
However, when I try to execute the script on actual data:
C:\>C:\Python27\python.exe C:\Python27\run_batch_job.py Sanger.txt Output.txt PositionConverter
Error:
usage: run_batch_job.py [-h] [-v] [-b BUILD] INPUT OUTPUT TYPE
run_batch_job.py: error: argument INPUT: can't open 'Sanger.txt':
[Errno 2] No such file or directory: 'Sanger.txt'
However, the Sanger.txt is in the Python27 directory. I have not been able to figure it out.
Thank you :).

Related

.sh file works in terminal but not in python script (rclone w/ Raspberry Pi)

I'm having trouble running a .sh file in python. When I type in the location of the .sh file (/home/pi/file/script.sh) the script runs perfectly.
I'm trying to run this script in my python2 script and I've done the following methods:
subprocess.Popen(['bash', 'location of .sh'])
subprocess.call(['location of .sh'])
os.popen(['location of .sh'])
When I run the python script, I get a prompt from rclone saying "Command sync needs 2 arguments maximum"
My .sh file just includes:
#!/bin/sh
sudo /usr/local/bin/rclone -v sync /home/pi/some_project_data remote:rclone --delete-before --include *.csv --include *.py
I'm not sure how running the .sh file on terminal works fine, but this error pops up when I'm trying to run the .sh file using Python.
Your script fails whenever you run it in a directory containing 2 or more .csv or .py files. This is true for terminals as well as via Python.
To avoid that, quote your patterns so the shell doesn't expand them:
#!/bin/sh
sudo /usr/local/bin/rclone -v sync /home/pi/some_project_data remote:rclone \
--delete-before --include "*.csv" --include "*.py"
Please try:
os.popen('bash locationof.sh')
ex:
os.popen('bash /home/script.sh')
That worked on my machine. If you place square brackets around the string then python assumes it is a list and popen doesnt accept a list, it accepts a single string.
If the script doesnt work, then this won't fix that, but it will at least run it. If it still doesnt work, try running the script with something like
touch z.txt
and see if z.txt appears in the file explorer. If it does, then your .sh file has a problem.

How do I run this python file in the command prompt when I'm getting an error message

I created this file ex1.py and saved it in the Python27 file which is on my C drive. I'm trying to run this file in the command prompt. On my command prompt by default it is set for PS C:\Users\Ted. Next I changed the path so that it was reflective of where the file is actually located and this is the error message I got "A positional parameter cannot be found that accepts argument ex1.py"
As per Anthony Forloney's comment:
... you do not need the PS before the C:\Python27\python argument. Also, you may not even need to provide the path to your python.exe if C:\Python27 is defined appropriately as your PATH environment variable. In any event, try: C:\Python27\python C:\directory\where\ex1.py
I found by changing the directory path to where the file was stored worked. So the syntax I used was cd C:\Python 27. Once I was in that directory I could then run the file.

Beginner Python spam.py

I am new to python, just got the learning python book and got stuck with the spam.py in the command line. The book says to make a file named spam.py and then ask python to run this by typing
%python spam.py
I have added the python to my PATH as it was C:\Python27 so I can call Python in the Windows CMD, but this just will not run. The error I receive is
>>>python spam.py
File "<stdin>", line 1
python spam.py
^
SyntaxError: invalid syntax
I appreciate any help that you can give.
Your problem is that you're trying to run your code from within the Python interpreter itself (the >>> prompt is the giveaway here since that's the Python prompt).
Exit from the interpreter (with CTRL-Z and ENTER for Windows) and run it from cmd.exe (the c:\> is the prompt in the example below):
c:\> python spam.py
From within the interpreter, you can also run an external file with:
execfile('spam.py')
Could you post the code from spam.py...
You seem to be trying to run the spam.py from the Python interpreter. Go to where the file is in Windows Explorer and launch it from there, using the C:\Python2.7\python.exe CLI.
By the way, since you didn't understand the syntax error warning, please see:
http://dictionary.reference.com/browse/SYNTAX
http://dictionary.reference.com/browse/SEMANTICS
You are trying to execute Python script file within the interpreter. Come out from the Python interpreter by pressing CTRL+Z and then ENTER key.
Then execute with the command :
Say, C:/> python spam.py
In order to run a python program you have to run program in Command Line not in Python Interpreter (press Windows Sign + R and type cmd.exe)
Moreover you have to remember to be exactly in the directory where your file is saved, e.g.:
If the file file is C:\Python27\spam.py you have to be in C:\Python27.
To change the directory:
use dir to display the folders and files in current place
use cd to change your directory (e.g. C:\Python27\>cd Spam moves you to C:\Python27\Spam
use Tab key to autocomplete names of the commands, folders and files
As you said you have added Python to PATH and followed my instructions, the statement below should work perfectly
python spam.py
Hope I could help.
If for example, your file is in G: drive, type this in cmd:
python G:/myfile.py
Basically, type in the path. Just doing "cd" won't work in Python

Installing Django "open file with" message when using the "setup.py install" command

I installed the latest version of Django from here: https://www.djangoproject.com/, unzipped the file and moved it to my desktop. Took note of the file path, opened the command prompt and changed the directory to that file path and then typed 'setup.py install' it first said 'open with' and I picked to open with python.exe (the python command line) and now it says 'error: no commands supplied'.. If I try 'python setup.py install' it says "python' is not recognized as an internal or external command, operable program or batch file". Any idea why?
You need to add the python.exe file to your system PATH.
Heres a quick link on how to set/update your path on windows http://www.computerhope.com/issues/ch000549.htm
Once you have that python.exe file added to your path you can run python from the command line.

Creating executable from python source file

I am following this guide for python 2.7 :
http://docs.python.org/tutorial/interpreter.html
I do all it says: I have a python file:
#! /usr/bin/env python
print "hello world !\n"
And from terminal, in the directory where is the file I type:
chmod +x hello_world.py
The file is name hello_world.py; But nothing happens, it doesn't print "hello world\n".
sorry if this is insultingly obvious, but
> chmod +x hello_world.py
only changes the file so that you can run it. next you need to actually run the file by typing:
> ./hello_world.py
hello world !
To give a bit more description: the chmod command changes the permissions of a file on a Unix-style system. The +x in the command:
chmod +x hello_world.py
Sets the "Executable" bit for the hello_world.py file, thereby making it a script which can be executed. Thus to run the script:
./hello_world.py
The ./ in front indicates that the file is in the current directory. Alternatively, you can always run a script by invoking the python interpreter directly (regardless of permissions) like so:
python hello_world.py