Python study notes ten _ module, third-party module installation, module import

1. Modules and packages

1. Module

A module is essentially a python file. It is used to organize code, which means to write python code in it, the file name is the name of the module, test.py test is the name of the module

2 bags

A package, a package is essentially a folder. Unlike a folder, it has an __init__.py file. A package organizes modules logically, that is to say, it is used to store modules. If you think of modules in other directories, then this directory must be a package before you can import it.

Second, the module classification

1. Standard modules, standard packages

  These modules that come with python can be used directly by importing

  import string,random,datetime,os,sys,json,hashlib

2. Third-party modules

  Some modules written by others can only be used after you have installed them.

  If you want to implement a certain function, you can go to Baidu to search for third-party modules.

3. Python file written by yourself

3. Install third-party modules

1. foolish

  (1) Enter the commands directly in the command line window pip install pymysql , pip install redis

    The version of pip above python3.4 comes with it. But this method needs to ensure that the Project Interpreter path in pycharm is the Python installation path, otherwise even if the window shows that the download and installation are successful, the import will still not be successful.

    Because the third-party installation package downloaded from the command line is stored in the Python installation path\python3\Lib\site-packages

    When pycharm import, the third-party installation package is taken from \python\venv\Lib\site-packages under the path set by pycharm->File->Settings->Project Interpreter->

  (2) You can also install third-party modules directly in pycharm, so that the installed modules will be placed under \python\venv\Lib\site-packages under the path set by Project Interpreter->, and they can be used directly.

  (3) If the prompt pip command does not exist

    enter where pip

    If it prompts that pip is not an internal command

   pycharm External Libraries

   Note: After python 3.5, scripts are automatically added to the computer environment variables   

How to do without pip command  

   a. Click python console b in pycharm
   , find the python installation directory
   c, and then add the scripts directory under the installation directory to the environment variable to
      ps: add
  (4) Unknown or unsupported command 'install' to the environment variable in the PATH How to solve this question
    a, open C:\strawberry\perl\bin\
    b, change pip in this directory to other names, this has no effect on others

2. Manual installation

When the external network cannot be downloaded directly, you can find someone else to download the package and install it manually

  (1) Baidu search: python redis

  (2) Find the URL: https://pypi.python.org/pypi/redis#downloads, download the installation package

  (3) Install the installation package ending with whl

   shift+right key, open the command line window here (or enter cmd directly in the address bar)

   pip install redis-2.10.6-py2.py3-none-any.whl

  (4) Install the installation package ending with tar.gz

    a. Unzip the compressed package

    b. Go to the unzipped folder (shift + right click, open the command line window here (or enter cmd directly in the address bar))

    c. Run python setup.py install on the command line

Fourth, import the module 

1. The order in which python imports modules:

  (1) Find the python file to be imported from the current directory

  (2) Find sys.path from python's environment variables 

2. The essence of the import module:

   It is to execute the python file from beginning to end once

for example:

  (1) Customize a python module dr.py and put it in the current directory 

name = 'hello'
def my():
    print('python')
my() 

  (2) Create a new tmp.py

import dr #When importing the file, the python file has been executed once, and python is printed out
print(dr.name) #Print out hello
dr.my() # print out python

The above code can also be written in the following way

from dr import name,my #When calling a custom function, you don't need to write "file name. function", just write the function or variable name directly
print(name)
my()
from dr import * #import all
#from aa import *
my()
#Try not to use it, because it is difficult to see which file the function belongs to when looking at the source code

  (3) When dr.py is placed under one of the environment variables of sys.path, dr. can click on the function 

    (4) There are dr.py in the current directory and the path environment variable, and the dr.py in the current directory will be selected first

Guess you like

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