python modules to import summary

python modules to import summary

 

Import module way

Defined test.py module

def print_func():
    print("hello")

The import statement

Import module syntax

import module1[, module2[,... moduleN]]

Reference print_func () function

Module name Function name
# ! / Usr / bin / Python 
# - * - Coding: UTF-8 - * - 
 
# import modules 
Import the Test 
 
# now contains the function modules can be called 
support.print_func ()

 

from ... import statement

Python's from statement lets you import a specified part from the module into the current namespace. The syntax is as follows:

from modname import name1[, name2[, ... nameN]]

For example, to import the module fib fibonacci function, using the following statement:

from fib import fibonacci

This assertion does not fib the entire module into the current namespace, where it will only fib fibonacci global symbol table into a single execution of the module of this statement.

 

from ... import * statement

All the contents of a module are all imported into the current namespace is also possible, simply use the following statement:

from modname import *

This provides an easy way to import all items in a module. However, this statement should not be too much to use.

For example, we want to introduce a one-time math module in all things, the statement is as follows:

from math import *

 

 

python absolute and relative paths

Transfer from https://blog.csdn.net/databatman/article/details/49453953

The following describes the path for the windows, other platforms are not being very understanding. 

Open the file in the file when prepared py wherein the following expression is often seen paths: 

Open ( ' aaa.txt ' )   
Open ( ' /data/bbb.txt ' )   
Open ( ' D: \\ \\ User CCC .txt ' )   
three inside the expression, the first two are the relative path, and the third is an absolute path. Absolute path is better understood, is the most complete path, relative path relative path is not complete, this refers to the relative relative to the current folder path, in fact, you write the py file put the folder path! That you write must be a relative path in the current folder A or A in a file folder in the file B before they can open. 

Py assumed that the current position is located folder: D: \ user \ public 

path so three lines open file belongs are: 

D: \ User \ public \ aaa.txt 

D: \ User \ public \ Data \ bbb.txt 

D: \ the User \ Private \ ccc.txt 

well understood is that when you want to open the file in which the py file just use relative paths on the line, but you need to use an absolute path to use other folders. 

Note: We used '/ 'To indicate relative path,' \ 'to indicate an absolute path, in the path \\ above is the escape it means. In addition, the page URL and linux, unix system usually use the '/ '. 

Of course, we can also obtain an absolute path to the current folder, as follows: 

Import os   
path1 = os.path.abspath ( ' . ' )    # Represents the absolute path of the current folder is located   
path2 = os.path.abspath ( ' .. ' )   # indicates that the current folder in which the absolute path to a folder   
so we often set a global variable to indicate the current path1 the absolute path, coupled with the relative path to open the file you want to open, in order to do so do not conflict on different platforms, because there are differences on different platforms represent a relative path.

 

PYTHONPATH variable

As an environment variable, PYTHONPATH by a number of directories contained in a list of components. PYTHONPATH shell variable PATH syntax and the same.

On Windows systems, typical PYTHONPATH as follows:

set PYTHONPATH=c:\python27\lib;

On UNIX systems, a typical PYTHONPATH follows:

set PYTHONPATH=/usr/local/lib/python

 

Search Path

When you import a module, Python parser search order for the location of the module are:

  • 1, the current directory
  • 2, if not in the current directory, Python is a shell variable PYTHONPATH search in each directory.
  • 3, if not find, Python will look at the default path. In UNIX, the default path is typically / usr / local / lib / python /.

Module search path is stored in the variable sys.path system module. Variable contains the current directory, PYTHONPATH and the default directory is determined by the installation process.

 

Set PYTHONPATH variable python

Python is an important PYTHONPATH environment variable, for introducing the module when the search path can be accessed by:

import sys

print(sys.path)
['C:\\Pycham', 'C:\\Pycham', 'C:\\Users\\Administrator\\my_site', 'C:\\Users\\Administrator\\mysite', 'C:\\Users\\Administrator\\test_login', 'C:\\Pycham\\anaconda\\Scripts\\python37.zip', 'C:\\ProgramData\\Anaconda3\\DLLs', 'C:\\ProgramData\\Anaconda3\\lib', 'C:\\ProgramData\\Anaconda3', 'C:\\Pycham\\anaconda', 'C:\\Pycham\\anaconda\\lib\\site-packages', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\win32', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\win32\\lib', 'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\Pythonwin']

The first element in the path list is an empty string, represents a relative path of the current directory.

Because when you import the module, the interpreter searches in the order of the list until you find the first module, the module priority imported as a module in the same directory.

The search path can be changed import modules here are two cases:

1, changed by sys.path.append (), sys.path.insert () method and the like, this method is restarted when the interpreter when setting the original failure .

import sys
  sys.path.append('/home/test/')

2, change the PYTHONPATH, this setting method permanent :

export PYTHONPATH=$PYTHONPATH:/home/test

In this case, the path may be the path through the displayed list sys.path member achieved: document

 

Guess you like

Origin www.cnblogs.com/-wenli/p/10990748.html