[Python learning] 2. Using Python on Linux

Source: Internet
Author: User
Tags virtual environment

This section describes how to use Python on the Linux platform.


1. Install Python.

Currently, most releases come with Python, so you do not need to install Python. If you want to install it, you can use the corresponding system installation command.

Fedora system: first log on as root and run yum install python

Ubuntu: for users in the root group, run sudo apt-get install python


2. Python scripts used

Linux is a file system. Which file is the Python used?

This can be obtained through the command: which python

For example, this is the result on my computer.

# Which python
/Usr/bin/python

Since this command exists, there may be more than one Python environment on the system.

I am currently encountering a situation where a micro-framework flask is used to reconstruct a python internally and add many features that I need.

In this case, you can use virtualenv to create a virtual environment and call your own Python in this virtual environment.


Therefore, you can write a python program without knowing the python path.

#/Usr/bin/env python

Which python is determined by the system?


3. python File Execution

Like most scripts, there are two methods. Take the file test. py as an example.

A. python test. py

Direct execution

B. chmod a + x test. py

./Test. py

Change to executable file and then execute


4. Use of python commands

From 3.a, we can see that python is A command in Linux. So what is its usage? This can be obtained through python -- help or man python. Is it similar to all Linux commands.

Currently, I usually use python-V to obtain the python version. In python programming, the version is a very important issue. Pay attention to whether your call or usage is supported by the version used.

By reading this user manual, we can find that python can add many parameters when executing files or commands. You can use them as needed.


5. Environment Variables for python Programming

Python has many environment variables, of which the following are important:

PYTHONHOME: indicates the location of python.

PYTHONPATH: Location of the python Library

Others can refer to https://docs.python.org/2/using/cmdline.html

Here, PYTHONHOME can be obtained through which python

PYTHONPATH can be obtained through sys. path. The following is an interesting experiment.

Here, the PYTHONPATH experiment process on my computer is as follows:

[Linxj @ localhost ~] $ Export PYTHONPATH =/home/linxj/Downloads/
[Linxj @ localhost ~] $ Python
Python 2.7.3 (default, Aug 9 2012, 17:23:58)
[GCC 4.7.1 20120720 (Red Hat 4.7.1-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> Import sys
>>> Sys. path
['', '/Home/linxj/Downloads', '/usr/lib/python27.zip', '/usr/lib/python2.7 ', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old ', '/usr/lib/python2.7/lib-dynload', '/usr/lib/python2.7/site-packages ', '/usr/lib/python2.7/site-packages/gst-0.10', '/usr/lib/python2.7/site-packages/gtk-2.0 ', '/usr/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']
>>>
KeyboardInterrupt
>>>
[4] + Stopped python
[Linxj @ localhost ~] $ Export PYTHONPATH =
[Linxj @ localhost ~] $ Python
Python 2.7.3 (default, Aug 9 2012, 17:23:58)
[GCC 4.7.1 20120720 (Red Hat 4.7.1-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> Import sys
>>> Sys. path
['', '/Home/linxj', '/usr/lib/python27.zip', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2 ', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old','/usr/lib/python2.7/lib-dynload ', '/usr/lib/python2.7/site-packages', '/usr/lib/python2.7/site-packages/gst-0.10 ', '/usr/lib/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']


For the time being, I would like to extend my understanding to you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.