Python创建模块及模块导入的方法
本文实例讲述了Python创建模块及模块导入的方法。分享给大家供大家参考。具体分析如下:
python学习手册中写道:
定义模块,只要使用文本编辑器,把一些python代码输入到文本中,然后以.py为后缀名进行保存,任何此类文件都会被认为是python模块。
比如说,下面的代码输入到一个文件中,就可以看作是一个模块:
def printme(var): print var if __name__ == '__main__': printme(1)
假设说输入到a.py中,那么import a就可以把这个模块导入。
然后可执行a.printme(3),屏幕即可打印出3:
>>> a.printme(3) 3 >>>
一个模块顶层定义的变量,会自动变成模块的属性。例如:
data=[1,2,3] def printme(var): print var if __name__ == '__main__': printme(1)
data变量就是模块的一个属性。其实printme也是一个属性,只不过是一个函数罢了。
引入模块示例如下:(假定此时data=[1,2,3]未定义)
>>> import a >>> a.data Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> a.data AttributeError: 'module' object has no attribute 'data' >>> reload(a) <module 'a' from 'C:/py\a.pyc'> >>> a.data Traceback (most recent call last): File "<pyshell#3>", line 1, in <module> a.data AttributeError: 'module' object has no attribute 'data' >>>
从上述提示可以看出data属性未定义,此时再在a.py文件中定义data=[1,2,3],重新加载a模块,并输出data属性:
>>> reload(a) <module 'a' from 'C:/py\a.py'> >>> a.data [1, 2, 3] >>>
这里的reload函数可以重新加载一个模块。如果在模块代码中更改了,那么需要重新加载。
上面a.data,就是访问模块中的属性。
上面的例子是导入一个文件作为一个模块。
其实python的模块导入还有更丰富的内容。
除了模块名之外,python也可以导入指定目录路径。python代码的目录就称为包。因此,这类导入就称为包导入。事实上,包导入是把计算机上的目录变成python的一个命名空间。而属性就是目录中包含的子目录或者是模块文件。
看下面例子:
在我的桌面上有一个aa文件夹,里面有bb文件夹,bb里面有a.py这个文件。
那么在aa和bb文件夹中分别放置一个__init__.py,之后,在命令行中import aa.bb.a,就可以导入模块a了。
希望本文所述对大家的Python程序设计有所帮助。

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

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

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











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.

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.

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.

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 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.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

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.

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".
