Why can't I run python scripts from the command line interactive session using "./ name.py"? - python-2.7

I'm following along with Google's Python class, and the person in the videos always runs his scripts from the interactive session in command line using "./". Whenever I try it, I just get a syntax error. How can I use ./ to run scripts? I'm using Windows 10

To run a script from the command line you need to use the syntax
python3 script.py
Now on Unix systems, it's possible to add a shebang to the first line of the script as followings
#!/usr/bin/env python3
This then allows the shell syntax './name.py' to work. But windows doesn't have this mechanism. Instead, you need to create an 'association' between the .py extension and the python executable ('right click', 'open with'). Or just use the full syntax. Both require the python executable to be in your path, and generally on windows both python 2 and 3 will have the same executable name

Related

How do I create a powershell script for windows that runs 2 python scripts

This is the first time I tried doing a shell script.
So this is what I got for now:
$pdf = read-host "enter the pdf name"
cmd /k C:\the path\\./PDF_ID.py $pdf /all> C:\the path\data.txt
C:\the path\pdf.py
So the first python script is executed and it saves the output to the data.txt but I dont know how to run the second python script that analyzes data.txt and outputs if the pdf file contains malware or not.
If you are able to run Python from Powershell, then python <scriptName>.py will get your job done.
You can give the full path of the Python executable within the command line (incase not working). You could check and ensure that your python executable path is available in your system variables.
If you are not looking from any concurrent solution, then directly put these two inside your samplePS1 file:
python firstscript.py ;
python secondscript.py ;
Note: If you want to pass arguments , then you can pass the arguments also. This is a sequential execution.
Hope it helps you.

Running a python script in windows

I am new to Python.
I am executing a script in windows, but it does nothing and gives an error "invalid syntax".
Can you help me with the error. I have set the PATH variable to include my python.exe location.
Please see attached image:
Your image is from the python command line, only python commands are accepted there and there is no need to specify python in the beginning (in the shell python is just a call to the python interpeter).
In order to run windows commands in python use the os or subprocess modules.

python not accessible in powershell v3 when opened with Popen

Hopefully this should be simple. Python environment is running fine if I open PowerShell v3 manually. I can check version and run external scripts etc. But as soon as I open powershell.exe through subprocess.Popen from a python script from another application, python simply won't run; "The term 'Python' is not recognised as the name of a cmdlet, function, script file or operable program... etc"
I've checked my environment paths repeatedly and python is running fine on the system in general.
anyone has any idea what this could be caused by?
subprocess.Popen(["powershell.exe", '-ExecutionPolicy', 'RemoteSigned', "path to PS1_script_with python command"])
My PS1 file looks like this:
cd C:\Users\David\Geeknote\geeknote-master\geeknote
python gnsync.py --path "C:\Users\David\Desktop\C4DtoEvernote", --mask "*.nfo", --notebook "Python Logs"
function Pause{Read-Host 'You have successfully synced your C4D Annotations to Evernote using gnsync.
Please press Enter to continue...' | Out-Null}
Pause{}
It seems (for whatever reason) your $PATH is not being read or honored by the process; and thus python cannot be found.
You can either:
Set up the path with $env:Path = "C:\Python27:C:\Python27\Scripts";
Setup the path using a custom console profile (ie, a .ps1 file) and passing it with -PSConsoleFile.
The simplest option, pass the full path to the Python executable in your command file C:\Python27\python.exe gsync.py ...
I would try #3, and then see if you need the other options.
Adjust the paths as appropriate - especially if you have multiple Python interpreters installed.

How Can I Execute A Python Script By Just Typing The Script's Name?

I have a python script that I want to execute from the terminal, but I don't want to use the command pythonscriptName.py, instead, I'd like to just type scriptName. Is that possible? If it is, how?
I had a look in here and here, but it doesn't work (probably because I'm on a different os).
I'm using python 2.7.9 on osx Yosemite (10.10.3).
Put this as the first line in your Python script:
#!/usr/bin/python
(or wherever your Python interpreter lives).
Then give the script the executable bit:
chmod +x scriptName.py
Now you should be able to execute it like ./scriptName.py. You can then put a symlink without the .py extension somewhere in your path.

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