I have a Python script that I would like to be able to execute from no matter where. Either in Linux or in Windows, but in this case preferably in Windows. Putting the path to the script into PATH under Windows did not work, so from some directory calling python my_script.py
results in the message that there is no such file in this directory. So, is this somehow possible?

- 3,804
- 3
- 30
- 60
-
Making a batch file in windows in some $PATH directory, would help? – Joel Mar 12 '16 at 21:52
-
can you elaborate or tell me what more exactly I need to search for? – lo tolmencre Mar 12 '16 at 21:53
-
Why can't you use `python c:\full\path\to\your_script.py`? This will work regardless of your current directory. – Sven Marnach Mar 12 '16 at 21:54
-
because being lazy is kind of the point of environment variables, isn't it? – lo tolmencre Mar 12 '16 at 21:55
-
Well, since you are scripting, I thought you know about batching in Windows. Batch is a windows especific file that perform task, like bash in linuxes. More help for this command [here](https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx?mfr=true). – Joel Mar 12 '16 at 21:55
-
So your actual question is how to create an alias for a long command. This is completely unrelated to environment variables, and is answered in Bernard Meurer's answer below. – Sven Marnach Mar 12 '16 at 21:56
-
@SvenMarnach I figured he meant an alias, beginners can sometimes misinterpret the right tool for the job, part of learning I guess. – Bernardo Meurer Mar 12 '16 at 21:59
-
@Joel - batch scripts may be a bit more limited than python. For reference, see almost everything about python. Batch scripts are also notoriously difficult to run on Linux. – tdelaney Mar 12 '16 at 22:29
3 Answers
You can try creating an alias as such:
Linux
- Create a
.bash_aliases
file on your home folder - Add an alias such as
alias pscript='python /home/pythonscript.py'
- Log out and back in or do a
source .bash_aliases
Windows
- Run
doskey pscript=python C:\script.py
. Read more here

- 1
- 1

- 2,295
- 5
- 31
- 52
-
Creating aliases for python scripts isn't very scalable. Why not just make the script executable? – tdelaney Mar 12 '16 at 22:37
-
@tdelaney Because that's not what you asked for? If you want Python scripts to be standalone executables check [py2exe](http://www.py2exe.org/) – Bernardo Meurer Mar 12 '16 at 23:05
-
I didn't ask for anything... OP wants to know about executing python scripts that are in the PATH. You seem unfamiliar with making scripts executable so you hack a solution by creating aliases the shell. But that is not usually how one makes a program available for execution. py2exe is not needed, all that's needed is to understand how to make scripts executable on various systems. – tdelaney Mar 12 '16 at 23:17
-
Mistook you for OP @tdelaney, my bad. I don't use aliases, I make a shebang and execute them normally, but then again what he wanted to do was to add a random dir to his PATH only to make them executable, and his comments indicated even further that all he wanted was an alias. Anyway, py2exe is not needed, but usable anyway – Bernardo Meurer Mar 12 '16 at 23:19
The PATH is used to search for executables, and this doesn't include in windows script files.
A workaround is to convert the script to a batch file, see here how to simply have the script act also as a batch file

- 1
- 1

- 8,149
- 7
- 58
- 106
-
The conversion thing worked, but putting the path to the my_script.bat containing the python code and the preamble to the path variables still results in an error about no such file. – lo tolmencre Mar 12 '16 at 22:14
-
not very clear, did you create the batch file (say, x.bat), but in a directory in your path, and then executing x.bat doesn't work? – Ophir Yoktan Mar 12 '16 at 22:16
-
I converted my python file to a bat file. Then I added the path to that bat file to my environment variables. Calling my_script.bat from some unrelated folder then results in the error. – lo tolmencre Mar 12 '16 at 22:19
-
Yes, you mean like for instance executing a python script? Yes. The bat file is on a drive other than C: if that matters... – lo tolmencre Mar 12 '16 at 22:27
-
no - place a different batch file or executable in this directory, and execute it the same way. I suspect that either you have a typo in the path, or maybe the path wasn't updated correctly – Ophir Yoktan Mar 12 '16 at 22:29
-
".bat" and ".cmd" files only work in Windows because the have file associations setup to run them. You can build a file association for .py files also. Once you do that, windows recognizes the script as an executable and will run it directly. – tdelaney Mar 12 '16 at 22:33
For the operating system to run your script, it needs to find it (the PATH variable), recognize that it is executable and know which program should execute it. You seem to have the PATH part handled, so now for the other two.
On unixy systems you need to make the script executable. To set the user-executable bit, do chmod u+x myscript.py
. Then you need to tell the system which program should run it. Typically you use the "shebang" as the very first line in the file:
#!/usr/bin/env python3
The system will search the path for a program called "python3" (use "python" for python 2 scripts) and use that executable to run the script.
On Windows, you need to associate the file extension (.py) with the python exeuctable. That's usually done for you when python is installed. If not, you can dig into ftype, assoc and pathext here.
Windows doesn't care about the shebang (unless you are running cygwin, then see unixy systems above) so the same script can live in both worlds.
Once the script is executable, you call it directly instead of executing python and giving the file as the script name. Its just
myscript.py

- 73,364
- 6
- 83
- 116