问题描述
有没有一种可移植的方法可以在不编写其完整路径的情况下从shell运行python脚本?
Is there a portable way to run a python script from a shell without writing its full path?
例如在Linux中,我想在主目录中
For example in Linux, I would like while in my home directory
cd ~
能够运行一个名为run.py的python脚本,例如〜/long/path/to/run.py,但是我想通过简单地输入来运行它
to be able to run a python script called run.py that is in say, ~/long/path/to/run.py, but I want to run it by simply typing
python run.py
代替
python ~/long/path/to/run.py
我希望找到一种包含类似于PATH变量的目录的搜索路径列表,以便python run.py在一个目录中运行它遇到的第一个run.py.
I would hope for some kind of search path list that contains several directories just like the PATH variable, so that python run.py runs the first run.py it encounters in one of the directories.
我曾考虑过将run.py变成可执行文件,并将其目录添加到系统PATH变量中,但是找不到使python脚本可执行的便携式方法.
I have considered turning run.py into an executable and adding its directory the system PATH variable, but could not find a portable way of making a python script executable.
编辑
问了一年后,我的菜鸟少了一点,我发现我的问题不是很清楚,没有什么意义,所以在提出问题后,我将澄清一些事情.
One year later after asking it, I am a bit less noob, and I see that my question was not very clear and did not make much sense, so after a question upvote I'll clarify some things.
1)便携式.
当我问这个问题时,我说是便携式的.但是,在这种情况下,可移植的含义尚不清楚,我也没有对其进行过多强调.
When I asked this I said portable. However what portable means is not clear in this case, and I did not give much emphasis to it.
-
平台:应可在POSIX(Linux,MacOS等)和Windows上运行
the platforms: should work on POSIX (Linux, MacOS, etc.) and Windows
这仍然没有多大意义,因为Windows使用cmd.exe
,而POSIX使用sh
,因此每个人都可以使用不同的语法运行命令.因此,我们可以说,最可移植的方法是将相同的输入同时输入到sh
和cmd.exe
,并在两种情况下都运行python脚本.在这种情况下,您可以从ANSI C system
函数运行相同的命令,该函数在POSIX上使用sh
在Windows上使用cmd
. ANSI C是Windows和POSIX通用的少数几个东西之一,在这种情况下,这个问题是有道理的.
this still does not make much sense since windows uses cmd.exe
, and POSIX uses sh
, so each one could run the commands with a different syntax. So let's say that the most portable thing possible would be to feed the same input to both sh
and cmd.exe
, running the python script in both cases. In this case, you could run the same command from an ANSI C system
function, which uses sh
on POSIX and cmd
on Windows. ANSI C being one of the few things that is common to Windows and POSIX, the question makes some sense in that case.
2)可执行
接下来,短语turning run.py into an executable
不太清楚.当时我正在谈论chmod +x run.py
的Linux策略,添加一个shebang #!/usr/bin/env python
,并在系统目录〜/long/path/to/PATH环境变量中添加其目录.但这不适用于Windows,因为Windows不支持像Linux这样的可执行文件元数据属性,并且/usr/bin/env在Windows中不一定存在.
Next, the phrase turning run.py into an executable
, is not very clear. By that I was talking about the Linux strategy of chmod +x run.py
, add a shebang #!/usr/bin/env python
, and adding its directory the system add ~/long/path/to/ the PATH enviroment variable. But then this won't work for windows because windows does not support an executable file metadata property like Linux and because /usr/bin/env does not necessarily exist in Windows.
3)扩展
最后,在我的脑海中,我希望找到一种不指定运行哪种文件的解决方案,因此,如果有一天我们决定制作一个文件,例如一个perl文件,则接口不会改变.
Finally, in my head I was hoping for a solution that does not specify what kind of file run is, so that if someday we decide to make it, say, a perl file, no interfaces would change.
因此,编写run.py
会很不好,因为它会指定文件类型.最好只写run
Therefore, writing run.py
would be bad because it would specify the filetype; it would be better to be able to write just run
推荐答案
如果包含run.py
的目录在模块搜索路径上(例如,PYTHONPATH
环境变量),则您应该能够像这样运行它:
If the directory containing run.py
is on the module search path (for example, PYTHONPATH
environment variable), you should be able to run it like this:
python -m run
这是-m
命令行选项上的文档:
Here is the documentation on the -m
command line option:
这篇关于如何在不指定完整路径的情况下可移植地运行Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!