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

Docker online and offline installation and common command operations

1. Test environment name Version centos 7.6 docke...

MySQL Server 8.0.3 Installation and Configuration Methods Graphic Tutorial

This document records the installation and config...

Docker network mode and configuration method

1. Docker Network Mode When docker run creates a ...

Navicat imports csv data into mysql

This article shares with you how to use Navicat t...

Solution to Linux not supporting all commands

What should I do if Linux does not support all co...

MySQL master-slave replication principle and points to note

Written in front I have been writing a special to...

Solve the cross-domain problem of get and post requests of vue $http

Vue $http get and post request cross-domain probl...

Front-end JavaScript operation principle

Table of contents 1. What is a JavaScript engine?...

WeChat applet implements waterfall flow paging scrolling loading

This article shares the specific code for WeChat ...

IDEA configuration process of Docker

IDEA is the most commonly used development tool f...

MySQL 8.0.2 offline installation and configuration method graphic tutorial

The offline installation method of MySQL_8.0.2 is...