The path to learning Python (12)

Source: Internet
Author: User
Tags parent directory

This section mainly introduces import! It is very useful to invoke the function of the module.


Importing modules is importing real-world code

The import package is the __init__ () file under the import package and the two are not the same

First, module definition.

Module It is a py file it is the code that is responsible for logically organizing the code inside it can have variables have functions there are classes have logical modules It is not a simple stack of these things but to implement the function creation module is the new Python file defines a module: in the other files to use this module so to invoke: module name point variable name Module name Point method name
Import Module_alex
Print (Module_alex.name)
Module_alex.say_hello ()
Like the way it is now, it just imports a module. What if I want to import many modules? --comma separated to prevent collisions and a way to import aliases:
Import Module_alex
From Module_alex import logger as Logger_from_alex


def logger ():
Print (' I am logger in current file ')

Logger ()
Logger_from_alex ()
Here is the essence of import is literally importing a module of the meaning of the module is a file a bunch of code such as import Module_alex it is the essence of the Module_alex this file explained again or Module_alex this file Run it over and then assign the result of the run to the Module_alex variable that module_alex the heap of code that defines a number of variables if you want to call this variable module_alex.name so call if you want to call this method Module_al Ex.logger this call
Import Module_alex

Print (Module_alex.name)
You can also use the From Module_alex import name, which is equivalent to simply explaining the name of the import call if you do not have to write Module_alex to write the name directly can be imported
From Module_alex import name

Print (name)
Add a point is to write the directory structure when the right-click on the new build is the package after the completion of the package to find that it automatically bring a file that is, the packaging is the bag and bag is the meaning of the folder or directory it is the __init__.py file as a feature of the module it is the file in the inside write The code is OK if it is a package it is a folder in the inside of the module is OK, the package is to logically organize the module import module is imported module name or from the module name of import variable name Method name that import package is also import package name OK imported package it actually The __init__ () file that is under the explanation package is imported either as an import module or an import package under a sibling path. How do I import it if it's not in the same directory? The statistics directory does not have to be imported because it is actually going to find the path when it's imported. Press Sys.path to find
Import Sys
Print (Sys.path)
One can find it again, and it throws an exception, so that to find Module_alex, you have to add its parent directory to Sys.path.
Print (Os.path.abspath (__file__))
Results:

/users/lucia_gaga/pycharmprojects/practice_lucia/bb/path.py

Get Superiors:
Print (Os.path.dirname (Os.path.abspath (__file__)))
Results:

/users/lucia_gaga/pycharmprojects/practice_lucia/bb

Just one layer at a to go up.

The path to learning Python (12)

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.