The order of importing modules, the essence of importing modules, the sys module

import sys

# print(sys.platform) #Determine the operating system

# print(sys.path) #python's environment variables

# sys.path.append(r'E:\syz\ly-code\day5') #Add environment variables

# sys.path.insert(0,r'E:\syz\ly-code\day5') #Add environment variables, and put the environment variable path in the first place

 

# print(sys.argv) #Used to get the parameters passed in when running the python file on the command line

sys.argv
  is used to get the parameters passed in when running the python file on the command line. It is a list.
  This list has one parameter by default, which is the current file name.

Example:
import sys
import os
command = sys.argv
print(command)
if len(command)>1:
  cmd1 = command[1]
  if cmd1=='--help':
    print('this is the help document'  
      'this python The file is used to explain the role of sys.argv')
  elif cmd1=='os':
    print('The current operating system is %s'%sys.platform)
  else:
    print('The input command is wrong')
else:
  print ('Pass in a parameter when running python'
    'eg '
    'python xx.py install ')

 


Note:
The order in which python imports modules:
  1. Find the python file to be imported from the current directory
  2. Find sys.path from the python environment variable

 

The essence of importing the module:
  this python file is executed from start to finish

  import nhy
  nhy.my()
  print(nhy.name)

  from nhy import my,name,
  my()
  print(name)

  Both ways are the same

  from nhy import * is deprecated

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325267568&siteId=291194637