Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize python script can be run directly in ubuntu

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to achieve python script in ubuntu can be run directly, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Let's take that translation program as an example. Last time, I told you about the packaging of applications under windows. This time, I will tell you that python files can be executed on their own under linux, so there is no need for python xxx.py.

Very simply, add the following sentence to the top of the python source file!

#! / usr/bin/python3

/ usr/bin/python3 is the directory where the python3 interpreter is located under ubuntu. You can check it with which python3.

Then add executable permissions to the python script after executing chmod + x. / xxx.py

For example, here I am

Sudo chmod + x. / translate.py

Finally run. / translate.py

And then. Wrong report

-bash:. / translate.py: / usr/bin/ python 3 ^ M: interpreter error: there is no such file or directory

Why?

This is the source code I wrote under win7, which was later transmitted to ubuntu with winscp, so it caused a problem. The code written under dos, taken to linux, is incompatible.

Solution:

Sudo vim translate.py:set ff=unix

And then execute. / translate.py

OK runs perfectly, but if you take a closer look, it doesn't seem perfect, because every time you have to change to the directory where the file is located to run. In order to use the program more effectively, we can connect translate.py to / usr/bin, or / usr/local/bin, and so on, in a directory configured with system environment variables.

Sudo ln-s / xxxx/xxxx/xxxx/translate.py / usr/local/bin/dict

/ xxxx/xxxx/xxxx/, which is the absolute path where translate.py is located

/ usr/local/bin/dict,dict is renamed after translate.py soft connection

Running effect

OK, will there be a ready-made translation program in my ubuntu system in the future? Isn't it beautiful!

Thank you for reading this article carefully. I hope the article "how to run the python script directly in ubuntu" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report