Creating a Message Generator on CentOS 7 w/ Python3
This Python automation program I put together asks the user a set of questions, set their answers as variables and prints out the response, it then pulls all the responses together for a motivating message booster. Please follow along and hope you enjoy!
For this to work, you will need a few things; as we gather the requirements we will walk though the installs and configurations. When complete, we would have completed these three prerequisites:
· A Linux CentOS 7 environment w/ Latest Patches
· Python Version 3 w/ Development Patch
· A Python3 Script (Program)
Follow along to see what you may need for yours.
1. CentOS 7 Environment:
My local Operating System is Windows 10, so I clicked here and added a New Server on Cloud Playground (Requires Login).
· Select Playground from menu.
· Select +New Server
· Select CentOS7 w/code-server for Distribution
· Select Medium: 3 units(s)[-2Virtual CPU,4 GiB Memory] for Size
· Click Create Server
When the server is ready, click into its properties by clicking the Distribution Server type. You will require these credentials to access your server; Username, Temp. Password and Public IPv4 that serves as the host.
Choose Instant Terminals on the menu bar and select Open SSH to login.
Note: At the Welcome page, use the Advanced SSH options usage example to access your server with Credentials identified above.
2. CentOS 7 Environment Latest Updates:
· After successfully logging on to your CentOS7 server, your first want to ensure the system files are up to date by running this command: sudo yum -y update
· It wouldn’t hurt to verify your yum-utilities are updated with the latest as well with this command: sudo yum -y install yum-utils
3. Installing Python3:
Because CentOS is derived from RHEL, the preinstalled version of Python is old with version 2 so therefore you will want to install IUS, which stands for Inline with Upstream Stable which provides Red Hat Package Manager (RPM) packages for some newer versions of select software.
· Enter this command: sudo yum -y install https://centos7.iuscommunity.org/ius-release.rpm (May receive Nothing to do response).
· Install Python3 with this command: sudo yum -y install python36u
· Verify its version with command: python3.6 –V
4. Python3 Development Patch:
Finally, we will need to install the IUS package python36u-devel, which provides us with libraries and header files we will need for Python3 development.
· Enter this command: sudo yum -y install python36u-devel
5. Setup your Environment or Virtual Environment for your programs (Optional):
Many programmers/developers like this idea as it ensures the programs being created and worked on doesn’t get integrated and mixed with important system files and avoids the potential of overwriting essential system files.
· Make a directory for the program space that will be worked on with command: mkdir “directory name”
· cd “directory name”
Note: If you’re not setting up a Virtual Environment, proceed now to 6. Python Code
· python3.6 -m venv my_env
· source my_env/bin/activate
(See screenshot here)
Note: Within the virtual environment, you can use the command python instead of python3.6, and pip instead of pip3.6 if you would prefer. If you use Python 3 on your machine outside of an environment, you will need to use the python3.6 and pip3.6 commands exclusively.
Your virtual environment is now ready to use.
6. Python Program Code:
Within the “Directory name” you created in Step 4 above, you will now create your program file, make it executable and write your code into the program, test and witness the results.
· Create Program File command: touch “your program name here”.py
· Make program executable: chmod 744 “your program name here”.py
· Write code using vim: vim “your program name here”.py
Test and Witness
Congratulations! At this point you have a Python 3 programming environment set up on your CentOS 7 server and can now say you completed a coding project!
Don’t let your learning stop here, check out these interesting guides: