How to install other versions of Python environment on Linux ECS with actual combat applet

Hello, everyone. I'm an old watch

A previous article Using Python to build a fund query robot, you can also expand! , python environment is required to be version 3.7 or above. Install Python on Linux for the first time (previously, Python 3.6.8 built in the system is used), and record it.

1. Download Python source code from the official website

It is suggested that the download speed is faster here, and then upload the file to the local server.

First, find the python version you want to download at the website below the local browser service, and click to download it.

https://www.python.org/downloads/source/

Note that the stable version is on the left and the pre release version is on the right. I choose Python 3 7.9, click Download XZ compressed source tarball.

We will upload the downloaded Python source code compressed package to the specified folder of the server through the pagoda (I created a soft folder under the root/Project folder). After uploading, you can directly right-click to decompress it in the pagoda.

If you don't know how to install and use the pagoda panel, you can see the previous article: The "pagoda" in Linux, the real pagoda! Detailed tutorial

2. Install Python

Next, we can continue to use the pagoda, click the terminal on the pagoda panel and enter the following instructions in turn.

  • cd enter Python source directory
  • . / configure --prefix sets the installation location
  • make compiles the source code and generates an executable file
  • make install installs the compiled executable file into the installation directory set through configure

The final installation directory I set here is / usr / local / Python 3 seven

cd /root/soft/Python-3.7.9
./configure --prefix=/usr/local/python3.7
make
make install

After the installation is successful, the installation directory will also be printed out. It will be prompted that this directory has not been added to the environment variable. We can add it or configure the soft link directly.

  • Method 1: configure environment variables

First, open the configuration file ~ /. With nano bashrc, add the last line of export path = "/ usr / local / Python 3.7 / bin: $path", save and exit, and finally run the configuration file.

nano ~/.bashrc
# After adding environment variables, run the next configuration file
source ~/.bashrc

But even so, you still have to set up soft links, or use Python 3 7 enter the python program.

  • Method 2: set soft link

The ln instruction is used here, and python3 7. The executable software is directly linked to python3 and pip3 in / usr/bin 7 is linked to pip3 in / usr/bin, so we can directly use Python 3. pip3 corresponds to version 3.7 directly.

  • ln -s indicates soft link
  • ln -f means mandatory linking. If other directories have been linked before, delete the previous link first and then make a new link
ln -sf /usr/local/python3.7/bin/python3.7 /usr/bin/python3
ln -sf /usr/local/python3.7/bin/pip3.7 /usr/bin/pip3

After setting, we can directly enter Python 3 -- version to see if the link is successful.

3. Simple use

  • Set mirror source

Create a pip folder, used to store pip configuration files.

mkdir .pip && cd .pip

We still use the nano instruction to edit the file,

nano pip.conf 

Copy the following contents into it, then press ctrl+o to save the file, and then press ctrl+x to exit the editing mode.

[global]
index-url = https://mirrors.aliyun.com/pypi/simple

[install]
trusted-host = mirrors.aliyun.com

In addition to alicloud images, you can also choose other image sources,

China University of science and technology https://pypi.mirrors.ustc.edu.cn/simple
 Watercress http://pypi.douban.com/simple
 Tsinghua University https://pypi.tuna.tsinghua.edu.cn/simple
 University of science and technology of China http://pypi.mirrors.ustc.edu.cn/simple
  • Install third party packages

The first thing to install is pipenv, which is convenient to create and manage the virtual environment. Install a request to crawl the data, and then install a pandas to process the data.

pip3 install pipenv rquests pandas 
  • Write a small program: generate random password

First, let's talk about the idea: randomly select the number of characters with a specified length from numbers, upper and lower case letters and special characters, and then form a string.

1> Character set

We can input by ourselves or use ready-made packages, such as string, which is a built-in package of Python and can quickly obtain various characters.

import string
string.printable
2> Random function

It is also a Python built-in package random, which mainly includes randint, random, randrange, choice, choices and other functions.

import random
random.choices('abc', k=2)
3> Random password generator
import string
import random
while True:
    try:
        password_len = int(input('Please enter the password length (only numbers):'))
        password = ''.join(random.choices(string.printable.strip(), k=password_len))
        print(f'Your new password is:{password},Please keep it~')
    except Exception as e:
        print(f'[[wrong] check whether the input is wrong. It may be non digital content. The error message is:{e}')
    print('*** Enter 0 if you want to end!!!***')
    print('*** Press enter to continue generating the new password ***')
    flag = input('Continue to generate new password:')
    if flag == '0':
        break
    print('******************************************')

There's time later. I'll optimize it. Hey, hey ~

Added by Terriator on Wed, 16 Feb 2022 03:48:06 +0200