我刚刚安装了一个 linux 系统(Kubuntu),想知道是否有一个程序可以使 python 程序在 linux 上可执行。
Just put this in the first line of your script :
#!/usr/bin/env python
Make the file executable with
chmod +x myfile.py
Execute with
./myfile.py
如果您想在 Python 中获得独立的二进制应用程序,请尝试使用 py2exe 或PyInstaller 之类的工具。
You can use PyInstaller. It generates a build dist so you can execute it as a single "binary" file.
http://pythonhosted.org/PyInstaller/#using-pyinstaller
Python 3 has the native option of create a build dist also:
Putting these lines at the starting of the code will tell your operating systems to look up the binary program needed for the execution of the python script ie it is the python interpreter.
So it depends on your operating system where it keeps the python interpreter. As I have Ubuntu as operating system it keeps the python interpreter in /usr/bin/python
so I have to write this line at the starting of my python script;
#!/usr/bin/python
After completing and saving your code
Start your command terminal
Make sure the script lies in your present working directory
Type chmod +x script_name.py
Now you can start the script by clicking the script. An alert box will appear; press "Run" or "Run in Terminal" in the alert box; or, at the terminal prompt, type ./script_name.py
If one want to make executable hello.py
first find the path where python is in your os with : which python
it usually resides under "/usr/bin/python" folder.
at the very first line of hello.py
one should add : #!/usr/bin/python
then through linux command chmod
one should just make it executable like : chmod +x hello.py
and execute with ./hello.py
I do the following:
This essentially turns the file into a bash executable. When you double-click it, it should run. This works in Unix-based systems.
Another way to do it could be by creating an alias. For example in terminal write:
alias printhello='python /home/hello_world.py'
Writing printhello
will run hello_world.py, but this is only temporary. To make aliases permanent, you have to add them to bashrc, you can edit it by writing this in the terminal:
gedit ~/.bashrc
Do the following steps:
#!/usr/bin/python
chmod +x <script-name>.py
<script-name>.py
from /usr/local/bin
ln -s /usr/local/bin/<executable-name-you-want> <path-to-your-script>
These steps works irrespective of whether you have single standalone python script or if you have multiple dependent script called by your main file.
On the top of your code first write:
#usr/bin/python
or #usr/bin/env python
Then create a new .sh
file called setup.sh
and write:
sudo apt-get install python python-pip
sudo chmod +x yourfile.py
sudo -H pip install librariesyouwant
Therefore removing the issue of not having python and installing any missing packages. To make a desktop shortcut do this:
sudo cp yourfile.py ~/Desktop/
sudo chmod +x ~/Desktop/yourfile.py
Please, note that if your code isn't made to create a GUI you will have to run your code from terminal with ./yourfile.py
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.