2

In Windows 8, I often use the Python Windows Launcher like

py C:/long/long/long/long/long/path/to/prog.py ...

Is there any way to set some environment setting, such as PATH or PYTHONPATH etc, to prevent having to type the full path to prog.py?

From my basic knowledge/research, PATH only helps with the py part of the command line and PYTHONPATH only helps with imports within prog.py, so how do I deal with the path to prog.py itself??

Notes:

  • I cannot modify the code, not even the "shebang" line, since it is needed to work on other platforms.
  • I cannot cd to the directory containing the programs to run them, because the programs will do something based on the directory they're run in (they'll modify the files in the directory they're run in).
  • I know that if I associate .py extension with the Python Windows Launcher, then I can run prog.py as the first item in the command line, and thus use PATH, but currently my .py extension is associated with my favorite editor and I'd like to keep it that way if possible (so I can double-click any Python file in Windows Explorer and edit it).
    • However, if someone suggests a solution where I can have a different association for Windows Explorer versus the command line, then that could be a potential solution! (i.e. in Windows Explorer, .py opens with the editor, while on command line, .py runs with Python Windows Launcher)
failwhale
  • 83
  • 1
  • 5
  • 1
    Can you not just `cd` into `C:/long/long/long/long/long/path/to/` and run `py prog.py`? I'm not entirely sure what the issue is. – Blender Aug 02 '15 at 05:55
  • @eryksun Sorry, I should have noted that my .py file extension is associated with my favorite editor and I'd like to keep it that way. (I'll edit the question with this note.) – failwhale Aug 02 '15 at 05:56
  • @Blender Sorry, the programs need to be run in other directories because it does things based on where it is run. I'll add that to the main question as well. – failwhale Aug 02 '15 at 05:57
  • Running a file from the shell command-line and double clicking in the GUI both execute the default verb, which is typically `open`. The default action for a script really should be to execute it. For editing you can add a command to the right-click menu. Otherwise you're making life difficult, but we can probably work around it with a batch file that loops over the output of `where prog.py` to get the full path to the script. – Eryk Sun Aug 02 '15 at 06:26
  • Here's a simple test to run on the command line, `for /f "delims=" %a in ('where /f prog.py') do @py %a`. This will run every prog.py found in the current directory and `PATH`. – Eryk Sun Aug 02 '15 at 06:46
  • Actually, I'm so silly. I could just set a variable for each program path, i.e.. `prog=C:/long/path/to/prog.py` and then do `py %prog% ...`. I guess I figured out an answer to my own question that was acceptable to me. – failwhale Aug 02 '15 at 07:15

2 Answers2

0

Answer to my own question: Actually, I'm so silly. I could just set a variable for each program path (there are only a few programs paths), i.e.. prog=C:/long/path/to/prog.py and then do py %prog% .... I guess I figured out an answer to my own question that was acceptable to me.

Update: I just found something even better. I can do

doskey prog=py C:/long/path/to/prog.py $*

and then simply prog ... afterward

Now I just have to do some crazy stuff to get the doskey command into a file that will be run every time I start a console, as described here: https://stackoverflow.com/a/21040825/5182136

Community
  • 1
  • 1
failwhale
  • 83
  • 1
  • 5
  • FYI, doskey aliases are implemented in the console, i.e. conhost.exe (NOT cmd.exe), based on matching an alias at the beginning of a line of input. They can't be used generically as commands, i.e. not in a batch file and not piped into (e.g. `echo input | prog`). – Eryk Sun Aug 02 '15 at 09:22
0

Add your long path to PYTHONPATH, then invoke your program as such:

python -m prog

Python will search for a module called prog and then run it as the main module.

Dunes
  • 37,291
  • 7
  • 81
  • 97
  • Hey, this works! This is way better than the solution I found, because it's general. (The solution I found works for me because I only have a few paths that I can hard code.) This is great. – failwhale Aug 02 '15 at 07:41
  • While this works, I did notice an issue though. If `prog.py` has a shebang line that specifies python3, then running `py path/to/prog.py` will automatically choose python3 since the Python Windows Launcher `py` recognizes the shebang line. However, if I run it as `py -m prog`, then `py` will ignore the shebang line and thus run `prog.py` in python2 by default. I end up having to run `py -3 -m prog`. This is not a deal-breaker, but I wonder if there's a nicer solution to this issue? – failwhale Aug 15 '15 at 19:57
  • I wouldn't know. I only have python 3 on my windows machine. For linux I just explicitly name the program I want to run the program with. ie. `python3 -m prog`. Most linux distros will install python2 and python3 as aliases for the respective versions of python. – Dunes Aug 17 '15 at 12:52