15_python Modular Programming _python programming Path

Source: Internet
Author: User

Before I talked to you about some of the data bases of Python, starting with this article, we began to formally learn the modular programming of Python.

Now let's explain what the module is called

I've already talked about how to define a method, if you're using a Python interaction (with an interactive device, or Ipython) to learn to define a method, you define the method, then exit the interaction, and then you use this method, obviously, it will not be successfully invoked, This is because the Python interface empties the memory after you exit, and all defined variables and methods are cleaned

If you want to use a defined method in your new script file, you must introduce a module mechanism

You can save the Python code you have written to a file (which, of course, ends with ". Py"), so you can use the methods and variables in this code for a long time.

A new file that wants to use a previously saved file must be imported through the import method.

Import

Import method is used to do module imports, he mainly has the following types of use

Import the entire module
In [1]: Import Sysin [2]: sys.pathout[2]:[', ' d:\\users\\susmote\\anaconda3\\scripts ', ' d:\\users\\susmote\\ Anaconda3\\python36.zip ', ' d:\\users\\susmote\\anaconda3\\dlls ', ' d:\\users\\susmote\\anaconda3\\lib ', ' D:\\Users \\susmote\\Anaconda3 ', ' d:\\users\\susmote\\anaconda3\\lib\\site-packages ', ' d:\\users\\susmote\\anaconda3\\lib\ \site-packages\\babel-2.5.0-py3.6.egg ', ' d:\\users\\susmote\\anaconda3\\lib\\site-packages\\win32 ', ' D:\\Users\\ Susmote\\anaconda3\\lib\\site-packages\\win32\\lib ', ' d:\\users\\susmote\\anaconda3\\lib\\site-packages\\ Pythonwin ', ' d:\\users\\susmote\\anaconda3\\lib\\site-packages\\ipython\\extensions ', ' C:\\Users\\susmote\\. Ipython ']

  

To import a method in a module
In [1]: from sys import Pathin [2]: pathout[2]:[', ' d:\\users\\susmote\\anaconda3\\scripts ', ' d:\\users\\susmote\\ Anaconda3\\python36.zip ', ' d:\\users\\susmote\\anaconda3\\dlls ', ' d:\\users\\susmote\\anaconda3\\lib ', ' D:\\Users \\susmote\\Anaconda3 ', ' d:\\users\\susmote\\anaconda3\\lib\\site-packages ', ' d:\\users\\susmote\\anaconda3\\lib\ \site-packages\\babel-2.5.0-py3.6.egg ', ' d:\\users\\susmote\\anaconda3\\lib\\site-packages\\win32 ', ' D:\\Users\\ Susmote\\anaconda3\\lib\\site-packages\\win32\\lib ', ' d:\\users\\susmote\\anaconda3\\lib\\site-packages\\ Pythonwin ', ' d:\\users\\susmote\\anaconda3\\lib\\site-packages\\ipython\\extensions ', ' C:\\Users\\susmote\\. Ipython ']

From the back of the module name, import followed by the specific method, you can connect multiple, separated by commas can

Import all methods directly
In [1]: from sys import *in [2]: pathout[2]:[', ' d:\\users\\susmote\\anaconda3\\scripts ', ' d:\\users\\susmote\\ Anaconda3\\python36.zip ', ' d:\\users\\susmote\\anaconda3\\dlls ', ' d:\\users\\susmote\\anaconda3\\lib ', ' D:\\Users \\susmote\\Anaconda3 ', ' d:\\users\\susmote\\anaconda3\\lib\\site-packages ', ' d:\\users\\susmote\\anaconda3\\lib\ \site-packages\\babel-2.5.0-py3.6.egg ', ' d:\\users\\susmote\\anaconda3\\lib\\site-packages\\win32 ', ' D:\\Users\\ Susmote\\anaconda3\\lib\\site-packages\\win32\\lib ', ' d:\\users\\susmote\\anaconda3\\lib\\site-packages\\ Pythonwin ', ' d:\\users\\susmote\\anaconda3\\lib\\site-packages\\ipython\\extensions ', ' C:\\Users\\susmote\\. Ipython ']

  

The above usage depends on the specific situation

Define your own module

I talked about the Fibonacci sequence when we were talking about defining the method, and this time we used him as an example.

First write these pieces of code in a text editor

Save As Fibo.py

Then we continue to open the Python interface in this directory

In [1]: Import Fiboin [2]: Fibo.fib ($) 0 1 1 2 3 5 8 144In [3]: Fibo.fib2 () out[3]: [0, 1, 1, 2, 3, 5, 8 , 13, 21, 34, 55, 89, 144]

You can directly import the previously written Python code, call the method written in it

__name__ variable

__NAME__ is a system variable that can display the name of the current function during execution

In short, __name__ is the global module name.

For example, the above example

In [1]: Import Fiboin [2]: Fibo.fib ($) 0 1 1 2 3 5 8 144In [3]: Fibo.fib2 () out[3]: [0, 1, 1, 2, 3, 5, 8 , 144]in, 4,,,,,,,,,,,,,,,,,,,,,,, and fibo.__name__out[4]: ' Fibo '

  

We will often use such an expression in the back.

If __name__ = __main__:

The global code is written at the end of this line of code, that is, the execution of the code will first determine whether it is executed locally, in short, he assured that your code can only run in your file

Module Path Change

The next thing we're going to talk about here is that it's very important that the Python file and the import module we added earlier are all in the same directory, so it will be executed successfully.

But if we're going to import python files from other paths, that's a bit of a hassle.

First, we need to familiarize ourselves with an order

In [1]: Import Sysin [2]: sys.pathout[2]:[', ' d:\\users\\susmote\\anaconda3\\scripts ', ' d:\\users\\susmote\\ Anaconda3\\python36.zip ', ' d:\\users\\susmote\\anaconda3\\dlls ', ' d:\\users\\susmote\\anaconda3\\lib ', ' D:\\Users \\susmote\\Anaconda3 ', ' d:\\users\\susmote\\anaconda3\\lib\\site-packages ', ' d:\\users\\susmote\\anaconda3\\lib\ \site-packages\\babel-2.5.0-py3.6.egg ', ' d:\\users\\susmote\\anaconda3\\lib\\site-packages\\win32 ', ' D:\\Users\\ Susmote\\anaconda3\\lib\\site-packages\\win32\\lib ', ' d:\\users\\susmote\\anaconda3\\lib\\site-packages\\ Pythonwin ', ' d:\\users\\susmote\\anaconda3\\lib\\site-packages\\ipython\\extensions ', ' C:\\Users\\susmote\\. Ipython ']

Commands that have been used many times before

This command shows the location of all the Python modules, returned as a list, and in layman's terms, the Python interpreter will go to these paths to find Python files, and each module

Like NumPy, the path to this module file

If we want to add our own module to the path, we can use the Append method (previously said Sys.path is a list)

For example, the following line of code

In [5]: Sys.path.append ("E:\\python-project\\python-project\\module_study") in [6]: sys.pathout[6]:[", ' d:\\users\\ Susmote\\anaconda3\\scripts ', ' d:\\users\\susmote\\anaconda3\\python36.zip ', ' d:\\users\\susmote\\anaconda3\\ DLLs ', ' d:\\users\\susmote\\anaconda3\\lib ', ' D:\\users\\susmote\\anaconda3 ', ' d:\\users\\susmote\\anaconda3\\lib \\site-packages ', ' D:\\users\\susmote\\anaconda3\\lib\\site-packages\\babel-2.5.0-py3.6.egg ', ' d:\\users\\ Susmote\\anaconda3\\lib\\site-packages\\win32 ', ' d:\\users\\susmote\\anaconda3\\lib\\site-packages\\win32\\lib ' , ' D:\\users\\susmote\\anaconda3\\lib\\site-packages\\pythonwin ', ' d:\\users\\susmote\\anaconda3\\lib\\ Site-packages\\ipython\\extensions ', ' C:\\users\\susmote\\.ipython ', ' e:\\python-project\\python-project\\module _study ']

Note that the escape character should be noted when entering the path, and the path has been successfully added to the list.

Let's test whether it succeeds or import the code of the Fibonacci sequence we defined earlier

First enter a directory, such as the C drive

And then into the Python interaction.

In [7]: Import Fiboin [8]: FIBO.FIB ($) 0 1 1 2 3 5 8 144In [9]: Fibo.fib2 () out[9]: [0, 1, 1, 2, 3, 5, 8 , 144]in, +,-----------[]: fibo.__name__out[10]: ' Fibo '

  

can be successfully imported

The simple thing about the module is these, more practice to be able to be born skillfully

Official Blog Www.susmote.com

15_python Modular Programming _python programming Path

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.