Python programming 2


基础永远值得花费90%的精力去学习加强。认识实践的重要性。

Ax Python Environment Setup | 环境

Python是跨平台的,学习设置它的环境。

Local Environment Setup

打开终端窗口输入“python”查看安装了哪个版本。

  • Unix (Solaris, Linux, FreeBSD, AIX, HP/UX, SunOS, IRIX, etc.)
  • Win 9x/NT/2000
  • Macintosh (Intel, PPC, 68K)
  • OS/2
  • DOS (multiple versions)
  • PalmOS
  • Nokia mobile phones
  • Windows CE
  • Acorn/RISC OS
  • BeOS
  • Amiga
  • VMS/OpenVMS
  • QNX
  • VxWorks
  • Psion
  • Python has also been ported to the Java and .NET virtual machines

Getting Python

最新版本的python可以在官网得到。

https://www.python.org/

可以下载python的文档来学习它。

https://www.python.org/doc/

Installing Python

Unix and Linux Installation

二进制安装

  • Open a Web browser and go to https://www.python.org/downloads/.
  • Follow the link to download zipped source code available for Unix/Linux.
  • Download and extract files.
  • Editing the Modules/Setup file if you want to customize some options.
  • run ./configure script
  • make
  • make install

安装的目录将在*/usr/local/bin* 下,以及库的目录 /usr/local/lib/pythonXX

Windows Installation

  • Open a Web browser and go to https://www.python.org/downloads/.
  • Follow the link for the Windows installer python-XYZ.msi file where XYZ is the version you need to install.
  • To use this installer python-XYZ.msi, the Windows system must support Microsoft Installer 2.0. Save the installer file to your local machine and then run it to find out if your machine supports MSI.
  • Run the downloaded file. This brings up the Python install wizard, which is really easy to use. Just accept the default settings, wait until the install is finished, and you are done.

Macintosh Installation

Recent Macs come with Python installed, but it may be several years out of date. See http://www.python.org/download/mac/ for instructions on getting the current version along with extra tools to support development on the Mac. For older Mac OS’s before Mac OS X 10.3 (released in 2003), MacPython is available.

Jack Jansen maintains it and you can have full access to the entire documentation at his website − http://www.cwi.nl/~jack/macpython.html. You can find complete installation details for Mac OS installation.

Setting up PATH

安装好后设置环境变量,好像高版本的默认帮你设置环境变量。

在linux下,将python添加到特定的路径下

  • In the csh shell − type setenv PATH “$PATH:/usr/local/bin/python” and press Enter.
  • In the bash shell (Linux) − type export PATH=”$PATH:/usr/local/bin/python” and press Enter.
  • In the sh or ksh shell − type PATH=”$PATH:/usr/local/bin/python” and press Enter.
  • Note − /usr/local/bin/python is the path of the Python directory

在windows中,打开环境变量找到Path添加。

Python Environment Variables

可以被python识别的环境变量

Sr.No. Variable & Description
1 PYTHONPATH 这个变量就是在哪里找库,找模块。
2 PYTHONSTARTUP python初始化的源代码文件。
3 PYTHONCASEOK 在windows中查找第一个不区分大小写的匹配项。
4 PYTHONHOME 可选模块搜索路径

Running Python

你有三种启动python的方法

Interactive Interpreter

直接在命令行启动。

命令行的可选参数:

Sr.No. Option & Description
1 -d 调试输出
2 -O 生成字节码
3 -S 不在启动时运行import site来查找python路径
4 -v 详细输出
5 -X 禁用基于类的内置异常
6 -c cmd 直接运行python语句
7 file 运行的文件

Script from the Command-line

$python script.py # Unix/Linux

or

python%(2||3) script.py # Unix/Linux

or

C: >python script.py # Windows/DOS

Integrated Development Environment

下载得心应手的编程工具是必不可少的。当然python也有自带的IDE。

  • Unix − IDLE is the very first Unix IDE for Python.
  • Windows − PythonWin is the first Windows interface for Python and is an IDE with a GUI.
  • Macintosh − The Macintosh version of Python along with the IDLE IDE is available from the main website, downloadable as either MacBinary or BinHex’d files.

文章作者: Enomothem
版权声明: 本博客所有文章除特别声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Enomothem !
  目录