简体   繁体   English

为在 Ubuntu 中执行 python 脚本创建终端名称

[英]Create terminal name for execute python script in Ubuntu

I have a python file in: '/home/username/scripts/pyscript' and I want set a word for execute directly this script.我在“/home/username/scripts/pyscript”中有一个 python 文件,我想设置一个词来直接执行这个脚本。

I want do this "python3 /home/username/scripts/pyscript/main.py arg1 arg2" but looks like this "myscript arg1 arg2"我想做这个“python3 /home/username/scripts/pyscript/main.py arg1 arg2”,但看起来像这个“myscript arg1 arg2”

Is this posible?这可能吗? Thank you anyway.还是非常感谢。

It is possibile in a number of ways.它可以通过多种方式实现。 Links are for Bash , supposedly your shell but the ideas always apply.链接适用于Bash ,据说是您的 shell 但这些想法始终适用。

First option: make a shell alias第一个选项:制作 shell别名

alias myscript='python3 /home/username/scripts/pyscript/main.py'

Be sure to add the alias to your.profile to make it survive logout.请务必将别名添加到 your.profile 以使其在注销后仍然有效。

Second option: define a wrapper script .第二个选项:定义一个包装脚本 Create a file with the following content, named after your desired command (eg myscript):创建一个包含以下内容的文件,以您想要的命令命名(例如 myscript):

#!/bin/bash
python3 /home/username/scripts/pyscript/main.py "$@"

save it and make it executable, then call it:保存并使其可执行,然后调用它:

chmod +x myscript
./myscript arg1 arg2

Be sure to copy the script in a folder in your PATH (check where with echo $PATH) to be able to call it from any folder.确保将脚本复制到 PATH 中的文件夹中(使用 echo $PATH 检查位置),以便能够从任何文件夹中调用它。

You can also use pyinstaller to create a single file executable:您还可以使用pyinstaller创建单个文件可执行文件:

Step 1: Install pyinstaller第 1 步:安装pyinstaller

[Note: best practice is to do this in a virutalenv ] [注意:最佳做法是在virutalenv中执行此操作]

$ pip install pyinstaller

Step 2: Run pyinstaller against your script第 2 步:针对您的脚本运行pyinstaller

$ pyinstaller --console --onefile /home/username/scripts/pyscript

$ pyinstaller pyscript.spec  # use this after the first run

Step 3: Test the generated executable第三步:测试生成的可执行文件

$ cd /home/username/scripts/dist  # generated by pyinstaller
$ pyscript arg1 arg2

Step 4: Leverage the $PATH variable第 4 步:利用 $PATH 变量

$ cp /home/username/scripts/dist/pyscript /usr/bin

You should now be able to run the executable from anywhere.您现在应该能够从任何地方运行可执行文件。

It should be noted that the executable that is generated is OS specific.应该注意的是,生成的可执行文件是特定于操作系统的。 For example, if you generate it on an Ubuntu machine, it will only run on Ubuntu (Debian based).例如,如果您在 Ubuntu 机器上生成它,它将仅在 Ubuntu(基于 Debian)上运行。 The same holds true for Windows and other Linux distros. Windows 和其他 Linux 发行版也是如此。

Finally I solver with the help of @pierpaciugo I add a alias at the end of the.bashrc for make it persistent:最后我在@pierpaciugo 的帮助下求解我在.bashrc 的末尾添加了一个别名以使其持久化:

alias create='bash /home/username/Programming/Python/GithubAPI/script.sh'

I couldn't use only alias because I have my python dependencies on a virtual environment so if I try this i could not add params to my python script.我不能只使用别名,因为我的 python 依赖于虚拟环境,所以如果我尝试这个,我无法将参数添加到我的 python 脚本中。

For that I create this bash script:为此,我创建了这个 bash 脚本:

#!/bin/bash

source /home/username/Programming/Python/GithubAPI/venv/bin/activate && python3 /home/username/Programming/Python/GithubAPI/main.py $@ && deactivate

Now I can write "create param1 param2" and it works.现在我可以编写“create param1 param2”并且它可以工作。 I am using all global paths but could be a good idea add the script in a folder in my PATH.我正在使用所有全局路径,但将脚本添加到我的 PATH 中的文件夹中可能是个好主意。

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

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