Home Backend Development Python Tutorial Python模块搜索概念介绍及模块安装方法介绍

Python模块搜索概念介绍及模块安装方法介绍

Jun 10, 2016 pm 03:11 PM
python

【import模块】

和C中的#include不同,Python中的import语句并不是简单的把一个文件插入另外一个文件。
导入其实是运行时的运算,程序第一次导入指定文件时,会执行以下步骤,

1. 找到模块文件
2. 编译成位码
3. 执行模块中的代码来创建所定义的模块

并生成.pyc字节码文件,这三个步骤只在程序执行时,模块第一次导入时会进行。之后导入相同的模块时,会跳过这三个步骤,而只提取内存中已加载的模块对象,速度要快的多。
NOTE:

1. Python把已加载的模块放在内置的sys.modules字典中,在import操作开始时会确认引用的模块是否已加载。如果想看哪些模块已加载,可以导入sys,打印sys.modules.keys()

复制代码 代码如下:

>>> sys.modules.keys() 
['heapq', 'functools', 'pyreadline.console.ansi',...,'pyreadline.modes'] 

2. 可以import的四种文件类型

1.源文件(.py)
2.字节码文件(.pyc)
3.C扩展文件(Windows: .dll或.pyd文件 | Linux: .so文件)
4.相同变量名的目录(用于包导入)

【搜索路径】

比如说:import math,不需要加路径和后缀名,系统会自动搜索路径,按照搜索先后顺序,列出模块搜索路径.

1. 程序的主目录(程序当前运行的目录)
2. PYTHONPATH目录(在环境变量里面配置与加到Path中一样)
3. 标准库目录(比如说: C:\Python27\Lib)
4. .pth文件所在内容(一般放在Lib\site-packages目录下,如下:easy-install.pth)

复制代码 代码如下:

import sys; sys.__plen = len(sys.path) 
./setuptools-0.6c11-py2.7.egg 
./py-1.4.26-py2.7.egg 
./sphinx-1.3b1-py2.7.egg 
./colorama-0.3.2-py2.7.egg 
./babel-1.3-py2.7.egg 
./snowballstemmer-1.2.0-py2.7.egg 
./docutils-0.12-py2.7.egg 
./pygments-2.0.1-py2.7.egg 
./jinja2-2.7.3-py2.7.egg 
./six-1.8.0-py2.7.egg 
./pytz-2014.9-py2.7.egg 
./markupsafe-0.23-py2.7.egg 
./pyzmq-14.4.1-py2.7-win-amd64.egg 
./tornado-4.0.2-py2.7-win-amd64.egg 
./backports.ssl_match_hostname-3.4.0.2-py2.7.egg 
./certifi-14.05.14-py2.7.egg 
./egg-0.2.0-py2.7.egg 
./chicken-0.1.0-py2.7.egg 
import sys; new=sys.path[sys.__plen:]; del sys.path[sys.__plen:]; p=getattr(sys,'__egginsert',0); sys.path[p:p]=new; sys.__egginsert = p+len(new) 

而所有这些路径可以通过sys.path来看到

复制代码 代码如下:

>>> import sys 
>>> sys.path 
['', 'C:\\Python27\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg', ...,'C:\\Python27\\lib\\site-packages\\wx-3.0-msw'] 

临时的工作路径加载,可以用
复制代码 代码如下:

>>> sys.path.append('d:\\test') 
>>> sys.path 
['', 'C:\\Python27\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg', 'C:\\Python27\\lib\\, 'd:\\test'] 
>>> sys.path.remove('d:\\test') 

【模块安装】

但上面的方法只是临时性的,一旦退出程序或当前交互环境,就失效。要想永久生效就必须让其加入到上述目录中
Perl的强大在于CPAN,可喜的是,Python也开始有类似的技术及功能,比如说pip,可以用包括pip来安装模块。

1. 自写模块,直接拷贝到上述任意目录
2. 第三方模块,执行python setup.py install安装
3. 用easy_install安装

下载ez_setup.py文件
执行: python ez_setup.py
安装所要安装的模块: easy_install py

4. 用pip安装

先用easy_install pip命令来安装pip
再用pip install Markdown来安装具体模块,假设模块名为: Markdown

5. 用第三方模块管理库(比如说Canopy就可以管理科学计算模块,可惜很多需要收费)

用此工具,就可以安装ipython环境了

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

See all articles