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

Specific use of lazy loading and preloading in js

Delayed loading (lazy loading) and preloading are...

Analysis and solution of abnormal problem of loading jar in tomcat

Description of the phenomenon: The project uses s...

Detailed explanation of Vue's seven value transfer methods

1. From father to son Define the props field in t...

Solve the error problem caused by modifying mysql data_dir

Today, I set up a newly purchased Alibaba Cloud E...

Automatic file synchronization between two Linux servers

When server B (172.17.166.11) is powered on or re...

Three ways to implement animation in CSS3

This is a test of the interviewee's basic kno...

Example of how to set WordPress pseudo-static in Nginx

Quoting Baidu's explanation of pseudo-static:...

Simple method to install mysql under linux

When searching online for methods to install MySQ...

Summary of the use of element's form elements

There are many form elements. Here is a brief sum...

Detailed explanation of the use of Linux time command

1. Command Introduction time is used to count the...

Detailed steps to install mysql in Win

This article shares the detailed steps of install...

Good website copywriting and good user experience

Looking at a website is actually like evaluating a...

Quickly solve the problem of slow Tomcat startup, super simple

Today I helped a classmate solve a problem - Tomc...