希望对某人有所帮助
您可能无法在全局范围内运行它,但如果您想在不进入 virtualenv 的情况下运行特定的 python 脚本。这是一个解决方法。
考虑到您在 linux 上使用它。
假设你有一个名为myenvname的virtualenv
您想在这个特定的myenvname 中运行特定的python 脚本myscript.py,甚至不使用终端,只需双击一个图标。
创建一个shell脚本myshellscript.sh
#!/bin/bash
# open the virtual environment
source /home/usr_name/.virtualenvs/myenvname
# location to the python script you want to run
# python/python3 depending on the version you are using
python location/to/your/python/script/myscript.py
授予myshellscript.sh 成为可执行文件的权限
chmod +x myshellscript.sh
在/usr/share/applications/ 中创建一个.desktop 文件
sudo nano /usr/share/applications/myscript.desktop
复制粘贴代码块并进行相应的更改
[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Exec=shellscript file to be run along with its path
Path=directory where the file is located
Name=myscript
Comment=comment here
Icon=icon path here
然后授予myscript.desktop的权限
chmod +x /usr/share/applications/myscript.desktop
你已经完成了。
只需转到/usr/share/applications/ 并双击myscript 图标,您的myscript.py 就会运行