[英]When using Python Windows Launcher, is there any way to prevent having to type full path?
In Windows 8, I often use the Python Windows Launcher like 在Windows 8中,我经常使用Python Windows启动器,例如
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
? 有什么方法可以设置某些环境设置,例如PATH或PYTHONPATH等,以防止必须输入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?? 根据我的基本知识/研究,PATH仅有助于命令行的py
部分,而PYTHONPATH仅有助于prog.py
内的prog.py
,那么如何处理prog.py
本身的路径?
Notes: 笔记:
.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). 我知道,如果我将.py
扩展名与Python Windows启动器相关联,则可以将prog.py
作为命令行中的第一项运行,从而使用PATH,但是目前我的.py
扩展名与我最喜欢的编辑器相关联,如果可能的话,希望将其保留下来(这样,我可以双击Windows资源管理器中的任何Python文件并对其进行编辑)。
.py
opens with the editor, while on command line, .py
runs with Python Windows Launcher) (即,在Windows资源管理器中, .py
与编辑器一起打开,而在命令行中, .py
与Python Windows启动器一起运行) 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), ie. 我可以为每个程序路径(只有几个程序路径)设置一个变量。 prog=C:/long/path/to/prog.py
and then do py %prog% ...
. prog=C:/long/path/to/prog.py
然后执行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 然后简单地prog ...
之后
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 现在,我只需要做一些疯狂的工作即可将doskey命令放入每次启动控制台时都会运行的文件中,如下所述: https : //stackoverflow.com/a/21040825/5182136
Add your long path to PYTHONPATH
, then invoke your program as such: 将您的长路径添加到PYTHONPATH
,然后像这样调用程序:
python -m prog
Python will search for a module called prog
and then run it as the main module. Python将搜索名为prog
的模块,然后将其作为主要模块运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.