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环境了

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



HadiDB: A lightweight, high-level scalable Python database HadiDB (hadidb) is a lightweight database written in Python, with a high level of scalability. Install HadiDB using pip installation: pipinstallhadidb User Management Create user: createuser() method to create a new user. The authentication() method authenticates the user's identity. fromhadidb.operationimportuseruser_obj=user("admin","admin")user_obj.

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.

As a data professional, you need to process large amounts of data from various sources. This can pose challenges to data management and analysis. Fortunately, two AWS services can help: AWS Glue and Amazon Athena.

The steps to start a Redis server include: Install Redis according to the operating system. Start the Redis service via redis-server (Linux/macOS) or redis-server.exe (Windows). Use the redis-cli ping (Linux/macOS) or redis-cli.exe ping (Windows) command to check the service status. Use a Redis client, such as redis-cli, Python, or Node.js, to access the server.

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.
