Build a python environment on CentOS 7.7 for your home server

Added on 2019.11.06.

motivation

I'm running a home server with GitLab and Mattermost installed on CentOS 7 to improve my programming environment at home (I'm preparing a separate article for this setup. It's been a long time due to various reasons ...)

Various notifications from GitLab can be seen on Mattermost, but other than that, if a virus is detected by ClamAV or an appropriate log can be sent to Mattermost's private channel, management will be easier. Gmail is buried in a lot of spam. It seems that python has a library called slackweb that can send messages to slack etc., so the final goal is to install it.

However, I didn't play with python in the server in the first place, so I will build an environment so that I can use 3 systems instead of the default 2.7 of CentOS 7. Since it is said to create an appropriate virtual environment so as not to pollute the original environment, when I googled it, the virtual environment by pyenv and pipenv seemed to be good, so I will try it for the time being.

Supplement to $ and # For the console, $ is the local user and # is the root user (sudo su state).

Advance preparation

First, as usual, update the environment with sudo yum update. This time GitLab went up from 12.4.0 to 12.4.1 and it took some time to update.

Install pyenv

First, install the necessary packages for pyenv.

# yum install gcc bzip2 bzip2-devel openssl openssl-devel readline readline-devel sqlite-devel tk-devel git

Then clone pyenv from github. Placed in / usr / local / pyenv so that it can be used by all users.

# git clone git://github.com/pyenv/pyenv.git /usr/local/pyenv
# cd /usr/local/pyenv
# mkdir {versions,shims}  <- ,Don't put a space after

Also, download the package for pyenv update.

# cd plugins/
# git clone git://github.com/pyenv/pyenv-update

Specify the environment variable for pyenv and pass it in the path.

# echo 'export PYENV_ROOT="/usr/local/pyenv"' >> /etc/profile.d/pyenv.sh
# echo 'export PATH="${PYENV_ROOT}/shims:${PYENV_ROOT}/bin:${PATH}"' >> /etc/profile.d/pyenv.sh

Also, PATH can be inherited at the time of sudo

# visudo

Above, edit the line with secure_path as follows.

#Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin
Defaults        env_keep += "PATH"
Defaults        env_keep += "PYENV_ROOT"

Now you can log in again or $ source /etc/profile.d/pyenv.sh and the path will pass so you can use pyenv.

$pyenv --version
pyenv 1.2.14-11-gb5f69fe
$pyenv install --list
~The available python versions are staggered~

(It's okay to go through the path, but I don't really understand why editing the contents of visudo is like this. Can I comment out things like / sbin ...?)

Installation of pipenv

Since pip itself was not included, install from there.

$curl https://bootstrap.pypa.io/get-pip.py | sudo python
$pip --version
pip 19.3.1 from /usr/lib/python2.7/site-packages/pip (python 2.7)

Next, wait for the installation of pipenv.

$sudo pip install pipenv
$pipenv --version
pipenv, version 2018.11.26

Create the virtual environment itself in the folder # 2019.11.06 Addendum

In the above state, pipenv can be used, and a virtual environment for each folder or project can be created with pipenv --python 3.7.5 etc. in the next section. However, the substance of this virtual environment is created in ~ / .local / share / virtualenvs /. When I logged in to the server, I wanted to get and notify who logged in to Mattermost with getpass.getuser (), but because the entity is in each user's home directory, I want to get slack web I couldn't read it and threw an error. Since the version of python was 3.7.5 for other users, it seems that the environment of pyenv was loaded, but the environment of pipenv could not be loaded. To avoid this, define $ PIPENV_VENV_IN_PROJECT as an environment variable. As a result, the substance of the virtual environment is stored in the .venv directory in each project directory. I want all logged-in users to use .venv of each project, so add the following to the end of / etc / bashrc.

/etc/bashrc


# To enable local virtual enviroment for pipenv
export PIPENV_VENV_IN_PROJECT=1

Now all users can handle everything from programs to environments that they want to use with just the contents of a single directory.

Building a python virtual environment with pyenv and pipenv

Create a project folder for checking the operation of slackweb, which is the initial goal.

$mkdir my_program
$mkdir my_program/slackweb
$cd my_program/slackweb

Specify python 3.7.5 of the latest 3.7 series board as the environment. However, it seems that libffi-devel is required to use 3.7 series or later, so install it before installing 3.7. (I went back and forth later without doing this)

$sudo yum install libffi-devel 
$sudo pyenv install 3.7.5

Now that 3.7.5 is installed via pyenv, create a virtual environment so that it can be used in the my_program / slack web folder.

$pipenv --python 3.7.5

In this state, check that the python version of system and the python version of pipenv in this folder are different.

$python --version
Python 2.7.5
$pipenv run python --version
Python 3.7.5

If you keep pipenv run in mind, you are using the command in that environment. That's fine with a little confirmation, but if you want to enter under the virtual environment and execute all subsequent commands with python in the virtual environment, if you want to execute all subsequent commands with python in the virtual environment, enter the environment with pipenv shell. When exiting, ʻexit`. Even if you do it from pipenv without doing pyenv first, it seems that it will be done from the installation of python in cooperation with pyenv, but in that case you have to do it with sudo. Then, the created virtual environment is likely to be under root (?), So this time it is executed separately. If you install pyenv only for local users and pass the path only to yourself, you will not need sudo (?)

Finally install slack web.

$pipenv install slackweb

If you do pip list in a normal shell, you will see this and that installed with pip in system, but in a virtual environment

[name@localhost slackweb]$ pipenv shell
(slackweb) [name@localhost slackweb]$ pip list
Package    Version
---------- -------
pip        19.3.1
setuptools 41.6.0
slackweb   1.0.5
wheel      0.33.6

In this way, you can check the contents of the virtual environment you created. Here's a test to see if slackweb works:

(slackweb) [name@localhost slackweb]$python
Python 3.7.5 (default, Nov  3 2019, 00:13:05)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import slackweb
>>> slack = slackweb.Slack(url="Inward webhook address for your Mattermost")
>>> slack.notify(text="This is a test from inside of the server.")

The goal was achieved because the message "This is a test from inside of the server." Was displayed on Mattermost's Town Square. This is convenient because various notifications can be sent to Mattermost in the future. Various things are likely to go on.

Make Mattermost login notification # 2019.11.06 Addendum

We have prepared a program that notifies Mattermost when you actually log in to the server.

login_notification.py


import slackweb
import getpass

slack = slackweb.Slack(url="Inward webhook address for your Mattermost")
attachments = []
attachment = {"title": "Login notification",
              "pretext": "This is a notification from _Server FQDN_",
              "text": "**" + getpass.getuser() + "** logged in.",
              "mrkdwn_in": ["text", "pretext"]}
attachments.append(attachment)
slack.notify(channel="#Notification destination channel name",
             username="CentOS 7",
             attachments=attachments)

By the way, the getpass module is python's default library, so you don't have to try to put it in with pipenv (or rather, you just get an error, you've wasted your time ...). If you inadvertently install it, a strange record will be left in the Pipfile, so delete unnecessary information with pipenv uninstall getpass, pipenv clean.

After that, if you execute this, you can get a login notification. Just do pipenv run python login_notification.py, but I want you to be notified automatically only once when you log in. Therefore, add it to the end of / etc / bashrc so that this also happens for all users.

/etc/bashrc


# Notify the login to mattermost
cd /home/username/my_program/slackweb/    <-username is the user who created the above environment and program
pipenv run python login_notification.py
cd ~     <-I don't know what to do later, so I'll go back to the logged-in user's home

Now, every time you log in with ssh or sudo su, Mattermost will be notified that you have logged in as each user or root. Mattermost can easily link notifications to smartphones, so even if a suspicious person takes over without your knowledge, you can immediately notice it. Since it is an insurance for the peace of mind, it is more important to prevent the private key for access from being leaked, but I think it is very important for human beings to feel at ease. (In that sense, I'm not at all relieved because I'm worried that I might inadvertently expose the webhook or user name when uploading this article.)

Reference site

Install Pyenv on CentOS and build Python 3 development environment

[Python] Build a python virtual environment with pyenv and pipenv (from almost the initial state of CentOS 7)

I tried to use Pipenv for Python development

Pipenv: Python Development Workflow for Humans (pipenv Official)

POST messages from python to Slack via incoming webhook

Recommended Posts

Build a python environment on CentOS 7.7 for your home server
Build a python3 environment on CentOS7
Build a python environment with ansible on centos6
Build a Python development environment on your Mac
Build a Python environment on your Mac using pyenv
Create a python environment on centos
Build a python environment on MacOS (Catallina)
Create a python environment on your Mac
Build a web server on your Chromebook
Build a Python + OpenCV environment on Cloud9
Build a Python environment on your Mac with Anaconda and PyCharm
Simply build a Python 3 execution environment on Windows
Build a Python environment on Mac (Mountain Lion)
Build a Kubernetes environment for development on Ubuntu
Building a Python environment on a Sakura VPS server
Build a Python development environment on Raspberry Pi
Build a version control environment for Python, Ruby, Perl, Node.js on UNIX
Build a LAMP environment [CentOS 7]
Build Python environment on Windows
Build python environment on windows
Build a Python environment offline
Build a GVim-based Python development environment on Windows 10 (3) GVim8.0 & Python3.6
# 2 Build a Python environment on AWS EC2 instance (ubuntu18.04)
Build a local development environment for Laravel6.X on Mac
Build a python environment for each directory with pyenv-virtualenv
Build a GVim-based Python development environment on Windows 10 (1) Installation
How to build a Django (python) environment on docker
Procedure for building a CDK environment on Windows (Python)
How to build a Python environment on amazon linux 2
Building a Python environment on Mac
Build a python machine learning study environment on macOS sierra
Build an environment for machine learning using Python on MacOSX
How to build a new python virtual environment on Ubuntu
Building a Python environment on Ubuntu
Create a Python environment on Mac (2017/4)
Build a GVim-based Python development environment on Windows 10 (2) Basic settings
Set up Python environment on CentOS
Build a Python environment and transfer data to the server
Build a Python extension for E-Cell 4 on Mac OSX (Yosemite)
Build Python 3.8 + Pipenv environment on Ubuntu 18.04
Create a virtual environment for python on mac [Very easy]
Create a Python environment for professionals in VS Code on Windows
Build and test a CI environment for multiple versions of Python
Build a 64-bit Python 2.7 environment with TDM-GCC and MinGW-w64 on Windows 7
Build a local development environment for Lambda + Python using Serverless Framework
# 3 Build a Python (Django) environment on AWS EC2 instance (ubuntu18.04) part2
Try using virtualenv, which can build a virtual environment for Python
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
Build a Pypi cache server on QNAP
Build an environment for Blender built-in Python
Python 2.7, 3.4, 3.5 extension module build environment on Windows
Build Python3 and OpenCV environment on Ubuntu 18.04
Let's create a virtual environment for Python
Build a simple WebDAV server on Linux
A memo with Python2.7 and Python3 on CentOS
Build CGI Server running on Python 3 on Docker
I want to build a Python environment
[Mac] Building a virtual environment for Python
Creating a python virtual environment on Windows
Build a Samba server on Arch Linux
Build Python environment with Anaconda on Mac