简体   繁体   English

如何通过仅在终端上键入名称来使python程序运行?

[英]How to make a python program run just by typing the name alone at the terminal?

I am not sure what I am supposed to be searching, because everything I have been trying to look up has not really given me the answers that I need. 我不确定应该搜索什么,因为我一直在尝试查找的所有内容并没有真正给我所需的答案。

If anyone can point me in the right direction of exactly what I should be looking at, I would greatly appreciate it. 如果有人能指出我应该看的正确方向,我将不胜感激。 Basically, I want to know the best way to go about making a python program which I can distribute (not publicly). 基本上,我想知道制作可分发(非公开)的python程序的最佳方法。

Once someone has the file they run a script, or run the make command, and the result of this would be that the user can invoke the program from within any directory on the computer by just typing the name (and passing args) in the terminal. 一旦有人获得了文件,他们将运行脚本或运行make命令,其结果将是用户可以通过在终端上键入名称(并传递args)从计算机上的任何目录中调用程序。 。

I only know of one way to do this, which I believe is the wrong way. 我只知道这样做的一种方法,我认为这是错误的方法。 My idea was something as follows: 我的想法如下:

1.) A main folder, let's call it Ghoul. 1.)一个主文件夹,我们称它为食尸鬼。 It would contain 3 files: 它包含3个文件:

 setup.sh ghoul.sh ghoul.py 

2.) The user would have to run setup.sh, which would make ghoul.sh an executable and it would change the name to ghoul and then I would move it to /bin 2.)用户必须运行setup.sh,这将使ghoul.sh成为可执行文件,并将名称更改为ghoul,然后将其移至/ bin

3.) Within the ghoul file which is now in /bin, the file itself would contain "python /home/user/Ghoul/ghoul.py $@" And this would allow the program to run from any directory just by typing "ghoul" 3.)在现在位于/ bin中的ghoul文件中,该文件本身将包含“ python /home/user/Ghoul/ghoul.py $ @”,这将使该程序可以从任何目录运行,只需键入“ ghoul “

I hope it's clear what I am trying to accomplish, I know this can't be the appropriate way of doing this. 我希望很清楚我要完成的工作,我知道这不是执行此操作的适当方法。 What should I be looking up and reading on to accomplish this? 我应该查找并继续阅读以完成此操作? I would like to know how I can do this on Ubuntu and if also ..Windows. 我想知道如何在Ubuntu和Windows上做到这一点。

I am also interested if I can limit the "ghoul" command to work only in the Ghoul directory, but of course in any directory that is within the Ghoul directory instead of from within any directory on the system. 我也很感兴趣是否可以限制“ ghoul”命令仅在Ghoul目录中使用,但是当然可以在Ghoul目录中的任何目录中使用,而不是在系统上的任何目录中。

Thank you. 谢谢。

On Unix like systems 在类似Unix的系统上

#! /usr/bin/env python

in the very first line of your chmod +x script file will make it executable from the current directory using ./filename 在chmod + x脚本文件的第一行中,将使用./filename从当前目录执行该文件

If you want completely straightforward execution from anywhere you can put it on the path somewhere however you choose. 如果您想从任何地方完全直接执行,则可以将其放在任意位置的路径上。

On windows its more complicated, if you want commandline execution the default install should give you the ability to execute 在Windows上较为复杂,如果您要执行命令行,则默认安装应使您能够执行

myscript.py

If its in your path. 如果它在您的路径上。 to drop the .py you need to get .py into the PATHEXT variable. 删除.py,您需要将.py放入PATHEXT变量。 Alternatively if it neds to be distributed to someone without any python install check out www.py2exe.org 或者,如果需要在不安装任何python的情况下将其分发给某人,请访问www.py2exe.org

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何仅通过在 Windows 10 cmd 行上键入脚本名称来运行 python 程序? - How do i run a python program just by typing the script name on windows 10 cmd line? 只需键入文件夹名称即可运行 python 模块 - run python module by just typing folder name 有没有办法制作一个让用户使用终端(NO GUI)成为Mac独立的Python程序? - Is there any way to make a Python program that has the user use the terminal (NO GUI) into a stand-alone for Mac? 如何创建一个 python 程序,从根终端运行调用 name 而不是 name.py? - How to create a python program that run calling name instead of name.py from root terminal? 如何在shell中运行python程序而不键入“python” - How to run a python program in a shell without typing “python” 如何让 Python 程序从终端连续运行(直到我手动停止它)? - How can I make a Python program run continuously from the Terminal (until I stop it manually)? 从终端运行python程序 - Run python program from terminal python 3打开终端并运行程序 - python 3 open terminal and run program 如何确保哪个程序在终端命令后运行 - How to make sure which program is run behind a terminal command 我如何让机器人只通过输入不带标签的用户名来提及某人 - How do i make the bot mention someone just by typing out the user's name without the tag
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM