Batch file to execute python script - python

I'm trying to write a batch file to execute a python script and not having much luck. I tried the following:
#echo off
SET path C:\"Program Files\python37\python.exe"
C:\"projects\systemcheck.py -c systems.csv"
but get the following error:
C:\projects>nexus-script.bat Environment variable path C:\"Program
Files\python37\python.exe" not defined 'C:\"projects\systemcheck.py -c
systems.csv"' is not recognized as an internal or external command,
operable program or batch file.

It may be easiest to specify the full path to the Python executable. That way you don't have to worry about the PATH environment variable.
#echo off
"C:\Program Files\python37\python.exe" C:\projects\systemcheck.py -c systems.csv
If you absolutely need to set the PATH environment variable, you would do it like this:
#echo off
SET "PATH=C:\Program Files\python37;%PATH%"
python C:\projects\systemcheck.py -c systems.csv
Note that the path to the Python folder goes before the previous contents of PATH; this ensures that that is the Python that gets run if you have multiple Python installations on you computer.

You need to have your FULL path in quotes. Example: SET path to "C:\Program Files\python37\python.exe"
You don't need to SET the path, you can do what Jack mentioned in a past comment.

Related

How do I make my python file executable from anywhere in my system and without having to say "python3" and ".py" before and after the file name

I am trying to make my python3 file executable from any location in the terminal (on mac os) so that instead of:
python3 myFile.py argv1 argv2 argv 3
Instead you type
myFile argv1 argv2 argv3
I can make the file executable via ./myFile but that's not really what I'm looking for and I can't make it executable from anywhere in the system.
Type which python3 to uncover where you have your python executable. In my case it is /usr/local/bin/python3 directory.
Your python file should contain a shebang referencing the executable as the first line in the file. So, for example:
#!/usr/local/bin/python3
Next, you must make sure that the file itself is executable.
Some files have different permissions (files can be read / write / execute and by different groups user / group / everyone). Execute on the terminal sudo chmod +x your_python_file.py.
Finally, you should add the directory where your file script is saved to your system $PATH.
In your terminal execute:
cd $HOME && mkdir bin
Put your python script in this bin directory. Then add the bin directory to your system $PATH by running:
export PATH="$HOME/bin:$PATH"
To make it executable without saying python first, put
#!/usr/bin/env python
as the first line and make the file executable.
To make it executable from any location, put the script in a directory that's in your $PATH environment variable.

How to know path of calling shell directory and use it in my python executable

Basically, I want to call a python program from anywhere using shell so I added a shebang and copied it to /usr/local/bin with executable permission. The python program takes a command line argument which is the relative path of an input file.
I am stuck here, I have no idea what to do so that I can obtain the absolute path of the shell. I am assuming once I get the absolute path somehow, I can use sys.argv[1] to get the entered relative path of the file(which I will append to the absolute path of shell working directory) but please do correct me it won't work.
You can Print Working Directory via os.environ['PWD']. Content of your_script.py:
#!/usr/bin/python3.5
import os
print(os.environ['PWD'])
Usage:
sanyash#sanyash-ub16:/etc/nginx$ your_script.py
/etc/nginx
sanyash#sanyash-ub16:/etc/nginx$

CMD can't normaly read 'C:\Program Files'

I have some problem with my WIndows CMD.
Some time I need to open python file using CMD command. And I write: 'C:\Program Files\Python X.X\python.exe file.py' but have error: 'C:\Program' isn't system command (maybe not the same, I have another OS language).
With different methods I have different errors but can't open python file.
Examples:
(Picture) translate: can't find 'C:\Program'...
(Picture) another example when I trying to write python directory first and then start python file, but it can't find python file.
Thanks for helping me.
There seems to be 2 different problems here.
Windows does not recognise spaces in directory or file names on the command line, so you need to put the directory insied "" .
i.e. "C:\Program Files\Python 3.4\python.exe"
In your second picture, suggests that run.py does not exist in the current directory. Change Directory to where the run.py file is before running that command.
First of all go to the directory where your python file is located ... like:
cd "c:\users\someone\documents\..."
On your pictures you are trying to run python file located in system32 folder but i guess it is not located there so move where the file is with that cd command
Then as Martin says the problem with path of python.exe is the space between words. To solve put the path into quotation marks.
But u can add python to system path and insted of writing full path u can write only
python file.py
How to add python to path see here https://superuser.com/questions/143119/how-to-add-python-to-the-windows-path

How do i append to the Maya PYTHONPATH?

I am working through Practical Maya Programming, and trying to set a 'development root' on my PC, I have followed the instructions (below) exactly but it is not working - At the point where I type 'mayapy.exe' I get the warning "'mayapy.exe' is not recognized as an internal or external command, operable program or batch file."
From the book:
Let's decide where we will do our coding. We'll call this location the development root for the rest of the book. To be concise, I'll choose C:\mayapybook\pylib to house all of our Python code.
Create the development root folder, and inside of it create an empty file named minspect.py.
Now, we need to get C:\mayapybook\pylib onto Python's sys.path so it can be imported. The easiest way to do this is to use the PYTHONPATH environment variable. From a Windows command line you can run the following to add the path, and ensure it worked:
> set PYTHONPATH=%PYTHONPATH%;C:\mayapybook\pylib
> mayapy.exe
>>> import sys
>>> 'C:\\mayapybook\\pylib' in sys.path
True
>>> import minspect
>>> minspect
<module 'minspect' from '...\minspect.py'>
EDIT
This is how it is working for me at the moment:
PS C:\Users\Me> set PYTHONPATH=%PYTHONPATH%;C:\mayapybook\pylib
C:\mayapybook\pylib : The term 'C:\mayapybook\pylib' is not recognized as the name of a cmdlet, function, script file,
or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and
try again.
At line:1 char:29
+ set PYTHONPATH=%PYTHONPATH%;C:\mayapybook\pylib
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\mayapybook\pylib:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
So the code from the book is not working but the code from the post by DrHaze seems to:
PS C:\Users\Me> setx PATH "%PATH%C:\mayapybook\pylib\"
SUCCESS: Specified value was saved.
But when I run the Maya Python Interpreter and check if C:\mayapybook\pylib\ is in sys path it returns false:
>>> 'C:\\mayapybook\\pylib' in sys.path
False
This error "'mayapy.exe' is not recognized as an internal or external command, operable program or batch file." means that the path where mayapy.exe is located is not included in the PATH environment variable. Your system tries to look in all the folders included in the PATH variable but can't find an executable called mayapy.exe.
The executable mayapy.exe is generally located here:
C:\Program Files\Autodesk\Maya(VERSION)\bin\mayapy.exe
on my computer it's located here: C:\Program Files\Autodesk\Maya2014\bin\mayapy.exe
To add the mayapy.exe location to your path, use one of the following commands:
setx PATH "%PATH%;C:\Program Files\Autodesk\Maya2014\bin\" if you
want to change it permanently
set PATH "%PATH%;C:\Program Files\Autodesk\Maya2014\bin\" only works for the current instance of the cmd session.
EDIT
The error you show in your edit is the source of the problem. Windows failed to set the environment variable PYTHONPATH. Hence, when you execute 'C:\\mayapybook\\pylib' in sys.path it returns False. sys.path is in fact containing the value of PYTHONPATH. That's why it returns False.
Now, why did it fail to set this environment variable?
First I can see that you are using Windows Powershell, keep this in mind.
The command I gave you is:
set PATH "%PATH%;C:\Program Files\Autodesk\Maya2014\bin\"
You wrote:
set PYTHONPATH=%PYTHONPATH%;C:\mayapybook\pylib
What it should be:
set PYTHONPATH "%PYTHONPATH%;C:\mayapybook\pylib\"
The syntax is a bit different and this last command should work.
As an explaination, your book gives you some commands to type in the vintage/old-style windows terminal: cmd.exe
As you are using Windows Powershell, some commands might have a different syntax.
Now what you can do is:
Use cmd.exe (Right click on the title bar -> Properties to custom it)
Use Powershell but keep in mind that the syntax might be a bit different than in your book
If you are using Powershell, there are different commands and strategies for managing environment variables.
You can set a variable permanently with SetEnvironmentVariable
You can set for the current shell session with: $env:VARNAME =
VARVALUE
You can put the commands to set variables in a powershell profile
file.
I would go with the third option. All three are detailed below:
Option 1. To append the directory "C:\mayapybook\pylib\" to the existing
PYTHONPATH permanently for your account:
[Environment]::SetEnvironmentVariable("PYTHONPATH", $env:PYTHONPATH +";C:\mayapybook\pylib\", "User")
Option 2. To append the Maya bin folder to your PATH for only the current
shell session:
$env:PATH += ";C:\Program Files\Autodesk\Maya2016\bin\"
Option 3. Create a powershell profile and set your env vars there.
First you'll need to make sure powershell scripts can run locally:
Hit the windows button, start typing powershell, right click and open as administrator. Enter:
Get-ExecutionPolicy
If it is says Restricted or AllSigned, set it to RemoteSigned like so:
Set-ExecutionPolicy RemoteSigned
Close that shell. Now in another powershell (not admin) type:
cd ~\Documents
md WindowsPowerShell
cd WindowsPowerShell
New-Item -path "profile.ps1" -type file
notepad.exe profile.ps1
Paste into the file any commands you want to run whenever a new powershell is opened:
Write-Host "Hello From Your Profile"
$env:PATH += ";C:\Program Files\Autodesk\Maya2016\bin\"
$env:PYTHONPATH += ";C:\mayapybook\pylib\"
Now whenever you open a powershell, you'll get a silly message and those paths will be set. You can test by typing:
Write-Host $env:PATH
or to list all env vars:
Get-ChildItem Env:
You should now be able to run commands from the maya bin directory. For example, type: maya to start maya.
Some other useful powershell env var commands here.

PYTHONPATH: command line calls on Windows 7

There are python scripts with command line arguments that I'd like to call from any location on my PC.
The idea is to share the corresponding package with others so they can open up a CMD window and run
python thescript.py arg1 arg2
regardless of their location.
How do I setup the python path/ PATH environment variables?
I've setup a package in site-packages, added that path to $PATH and edited PYTHONPATH to include the module directory (which includes __init__.py), but CMD won't find the relevant scripts.
python: can't open file 'thescript.py': [Errno 2] No such file or directory
Thanks.
Python does not look up scripts on some sort of path.
You have 2 options:
Use the full path:
python /path/to/thescript.py
Place the script in a directory that is on your PATH, make it executable (chmod +x thescript.py) and give it a Shebang line:
#!/bin/env python
The second option is probably preferable. On Windows, you can install pylauncher to support shebang lines; if you use Python 3.3 or newer, it is already included with your Python installation.
Another option would be to create a batch file for each script you care about, and put the batch file somewhere in your PATH, e.g. create a file called thescript.bat containing...
#echo off
the\path\to\python.exe the\path\to\thescript.py %*
...then you can just run...
thescript arg1 arg2
...which is about as terse a syntax as possible.

Categories