设为首页 收藏本站
查看: 1028|回复: 0

[经验分享] CentOS7安装Python3-Beleaguered Castle-运维网 博客

[复制链接]

尚未签到

发表于 2019-2-16 08:45:34 | 显示全部楼层 |阅读模式
Introduction

  Python is a versatile programming language that can be used for many different programming projects. First published in 1991 with a name inspired by the British comedy group Monty Python, the development team wanted to make Python a language that was fun to use. Easy to set up, and written in a>  This tutorial will guide you through installing Python 3 on your local CentOS 7 machine and setting up a programming environment via the command line.
Prerequisites
  You will need a CentOS 7 computer with a non-root superuser account that is connected to the internet.
Step 1 — Preparing the System
  We will be completing this installation through the command line. If your CentOS 7 computer starts up with a Graphical User Interface (GUI) desktop, you can gain access to the command line interface through the Menu, by navigating to Applications, then Utilities, and then clicking on Terminal. If you need more guidance on the terminal, be sure to read through the article “An Introduction to the Linux Terminal.”
  Before we begin with the installation, let's make sure to update the default system applications to have the latest versions available.
  We will be using the open-source package manager tool yum, which stands for Yellowdog Updater Modified. This is a commonly used tool for working with software packages on Red Hat based Linux systems like CentOS. It will let you easily install and update, as well as remove software packages on your computer.
  Let’s first make sure that yum is up to date by running this command:
sudo yum -y update  The -y flag is used to alert the system that we are aware that we are making changes, preventing the terminal from prompting us to confirm.
  Next, we will install yum-utils, a collection of utilities and plugins that extend and supplement yum:
sudo yum -y install yum-utils  Finally, we’ll install the CentOS Development Tools, which are used to allow you to build and compile software from source code:
sudo yum -y groupinstall development  Once everything is installed, our setup is in place and we can go on to install Python 3.
Step 2 — Installing and Setting Up Python 3
  CentOS is derived from RHEL (Red Hat Enterprise Linux), which has stability as its primary focus. Because of this, tested and stable versions of applications are what is most commonly found on the system and in downloadable packages, so on CentOS you will only find Python 2.

  Since instead we would like to install the most current upstream stable>  To install IUS, let’s install it through yum:
sudo yum -y install https://centos7.iuscommunity.org/ius-release.rpm  Once IUS is finished installing, we can install the most recent version of Python:
sudo yum -y install python36u  When the installation process of Python is complete, we can check to make sure that the installation was successful by checking for its version number with the python3.6 command:
python3.6 -V  With a version of Python 3.6 successfully installed, we will receive the following output:
OutputPython 3.6.1  We will next install pip, which will manage software packages for Python:
sudo yum -y install python36u-pip  A tool for use with Python, we will use pip to install and manage programming packages we may want to use in our development projects. You can install Python packages by typing:
sudo pip3.6 install package_name  Here, package_name can refer to any Python package or library, such as Django for web development or NumPy for scientific computing. So if you would like to install NumPy, you can do so with the command pip3.6 install numpy.
  Finally, we will need to install the IUS package python36u-devel, which provides us with libraries and header files we will need for Python 3 development:
sudo yum -y install python36u-devel  The venv module will be used to set up a virtual environment for our development projects in the next step.
Step 3 — Setting Up a Virtual Environment
  Now that we have Python installed and our system set up, we can go on to create our programming environment with venv.
  Virtual environments enable you to have an isolated space on your computer for Python projects, ensuring that each of your projects can have its own set of dependencies that won’t disrupt any of your other projects.
  Setting up a programming environment provides us with greater control over our Python projects and over how different versions of packages are handled. This is especially important when working with third-party packages.
  You can set up as many Python programming environments as you want. Each environment is basically a directory or folder in your computer that has a few scripts in it to make it act as an environment.
  Choose which directory you would like to put your Python programming environments in, or create a new directory with mkdir, as in:
mkdir environmentscd environments  Once you are in the directory where you would like the environments to live, you can create an environment by running the following command:
python3.6 -m venv my_env  Essentially, this command creates a new directory (in this case called my_env) that contains a few items that we can see with the ls command:
bin include lib lib64 pyvenv.cfg  Together, these files work to make sure that your projects are isolated from the broader context of your local machine, so that system files and project files don’t mix. This is good practice for version control and to ensure that each of your projects has access to the particular packages that it needs.
  To use this environment, you need to activate it, which you can do by typing the following command that calls the activate script in the bin directory:
source my_env/bin/activate  Your prompt will now be prefixed with the name of your environment, in this case it is called my_env:
  This prefix lets us know that the environment my_env is currently active, meaning that when we create programs here they will use only this particular environment’s settings and packages.
  Note: Within the virtual environment, you can use the command python instead of python3.6, and pipinstead 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.
  After following these steps, your virtual environment is ready to use.
Step 4 — Creating a Simple Program
  Now that we have our virtual environment set up, let’s create a simple “Hello, World!” program. This will make sure that our environment is working and gives us the opportunity to become more familiar with Python if we aren’t already.
  To do this, we’ll open up a command-line text editor such as vim and create a new file:
vi hello.py  Once the text file opens up in our terminal window, we will have to type i to enter insert mode, and then we can write our first program:
print("Hello, World!")  Now press ESC to leave insert mode. Next, type :x then ENTER to save and exit the file.
  We are now ready to run our program:
python hello.py  The hello.py program that you just created should cause the terminal to produce the following output:
OutputHello, World!  To leave the environment, simply type the command deactivate and you’ll return to your original directory.
Conclusion
  Congratulations! At this point you have a Python 3 programming environment set up on your local CentOS 7 machine and can begin a coding project!
  To set up Python 3 on another computer, follow the local programming environment guides for Ubuntu 16.04, Debian 8, macOS, or Windows 10. You can also read about installing Python and setting up a programming environment on an Ubuntu 16.04 server, which is especially useful when working on development teams.
  With your local machine ready for software development, you can continue to learn more about coding in Python by following “Understanding Data Types in Python 3” and “How To Use Variables in Python 3”.



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-672931-1-1.html 上篇帖子: 实验:CentOS下构建私有CA 下篇帖子: linux基础之centos7
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表