How to make Python scripts run directly under Ubuntu

How to make Python scripts run directly under Ubuntu

Let’s take the translation program as an example. Last time, I talked to you about application packaging under Windows. This time, I will tell you that Python files can be executed by themselves under Linux, so there is no need for python xxx.py.

It's very simple, just add the following sentence at 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 use which python3 to view it.

Then execute chmod +x ./xxx.py to add executable permissions to the Python script

For example, here

sudo chmod +x ./translate.py

Finally run ./translate.py

Then... an error was reported

-bash: ./translate.py: /usr/bin/python3^M: interpreter error: no such file or directory

Why?

This is the source code I wrote under Win7, and then transferred it to Ubuntu using WinSCP. This caused a problem: the code written under DOS is incompatible with Linux.

Solution:

sudo vim translate.py
:set ff=unix 

Then execute ./translate.py

OK, it runs perfectly, but if you look closely, it seems not perfect, because every time you have to switch to the directory where the file is located to run it, in order to use the program more conveniently, we can connect translate.py to /usr/bin, or /usr/local/bin, etc., in the directory where the system environment variables are configured.

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

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

/usr/local/bin/dict, dict is the name of translate.py after soft linking

Operation effect

OK, won’t there be a ready-made translation program in my Ubuntu system from now on? Isn’t it wonderful!

The above method of implementing Python scripts to run directly under Ubuntu is all the content that the editor shares with you. I hope it can give you a reference, and I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Ubuntu+python saves nii images into png format
  • Tutorial on installation and use of Python virtual environment under Ubuntu system
  • Switching between Python 2.7 and Python 3.6 environments in Ubuntu 18.04
  • How to switch Python versions in Ubuntu 16.04
  • Solution for perfect switching of Python versions under Ubuntu 18.04
  • Example method of installing Python on Ubuntu

<<:  Why do select @@session.tx_read_only appear in DB in large quantities?

>>:  How to implement real-time polygon refraction with threejs

Recommend

MySQL 5.7.27 installation and configuration method graphic tutorial

The installation tutorial of MySQL 5.7.27 is reco...

Detailed explanation of how to reduce memory usage in MySql

Preface By default, MySQL will initialize a large...

The benefits and examples of placing the site map at the bottom of the web page

In the past, almost every website had a sitemap p...

Five ways to achieve automatic page jump in HTML

In the previous article, we introduced three comm...

Detailed tutorial on installing MYSQL under WINDOWS

1. Download the installation package -Choose the ...

Tips for designing photo preview navigation on web pages

<br />Navigation does not just refer to the ...

Creating private members in JavaScript

Table of contents 1. Use closures 2. Use ES6 clas...

MySQL database query performance optimization strategy

Optimize queries Use the Explain statement to ana...

How to build a private Docker repository using Harbor

Table of contents 1. Open source warehouse manage...

CnBlogs custom blog style sharing

After spending half the night on it, I finally ma...

How to build a SOLO personal blog from scratch using Docker

Table of contents 1. Environmental Preparation 2....