简体   繁体   English

使用Python Windows启动器时,是否有任何方法可以防止键入完整路径?

[英]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: 笔记:

  • I cannot modify the code, not even the "shebang" line, since it is needed to work on other platforms. 我不能修改代码,甚至不能修改“ shebang”行,因为它需要在其他平台上工作。
  • 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). 我知道,如果我将.py扩展名与Python Windows启动器相关联,则可以将prog.py作为命令行中的第一项运行,从而使用PATH,但是目前我的.py扩展名与我最喜欢的编辑器相关联,如果可能的话,希望将其保留下来(这样,我可以双击Windows资源管理器中的任何Python文件并对其进行编辑)。
    • 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! 但是,如果有人建议一个解决方案,使我可以为Windows资源管理器和命令行建立不同的关联,那么这可能是一个潜在的解决方案! (ie in Windows Explorer, .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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM