The use of pip in python (requests/virtualenv/json)

Keywords: pip JSON Python Linux

1. How to install related packages using pip

  • pip install package==1.8.0 # specified version
  • pip install Package The latest stable version
  • pip install package name >= 1.4 # Minimum version that meets the criteria
  • pip list --outdated
  • pip install --upgrade package # update package
  • pip install --upgrade -i https://pypi.doubanio.com/simple/ package 6550
    • Source changeover-i,--index-url

2.Requirements file

  • requirements contain packages that need to be installed through pip:

      pip install -r requirements.txt
  • Four common methods of requirements:

    • Backup and restore the package environment through pip freeze.

      pip freeze > requirements.txt
      pip install -r requirements.txt
      
      PS C:\Users\xiaodong> python -m pip freeze > F:\requirements.txt
    • Mandatory resolution of dependency problems (not really solving dependency problems).
    • Force the use of alternate versions of child dependencies.
    • The Constranints file is similar to requirements, but it does not specify the version that must be installed, but constrains the installed version.

      pip install -c constranints.txt

3. Installation with Wheel

  • Wheel is a packaged program that has been built. Using Wheel packages to install third-party packages can greatly speed up the installation and reduce the difficulty of installation.

      pip install package-1.0-py2.py3-none-any.whl
    • Note: The file name of the wheel package cannot be changed by itself.

4. unloading package

  • Description: When pip updates a package, it will automatically start uninstalling and install a new version.

      pip uninstall package

5. packets of information

  • View a list of all installed packages

      pip list
  • View all expired packages and display the latest current version

      pip list --outdated
  • Packet details

      pip show package_name
  • Online Search Pack

      pip search "pkg" #Search starts with PyPI by default

6.VirtualEnv - Virtual Environment Building (linux System)

  • Install virtualenv Library

      pip install -i https://pypi.douban.com/simple/ virtualenv
  • Creating virtual environment Python 2_env

      Virtualenv python2_env #python2_env is a directory to create a virtual environment
  • Activating Virtual Environment

      source python2_env/bin/activate
  • Withdrawal from virtual environment (inactivation)

      deactivate
  • Specify a specific Python version

      Virtualenv -p python2 python2_env

7.requests -- Universal Request Library

  • Install requests

      sudo pip install -i https://pypi.douban.com/simple/ requests
  • Construct request

      import requests
    
      URL = 'http://fanyi.youdao.com/translate? smartresult=dict&smartresult=rule&smartresult=ugc&sessionFrom=null'
    
      headers = {
              'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
              'X-Requested-With':'XMLHttpRequest'
          }
      data = {
              'type':'AUTO',
              'i':'Come on'.encode('utf-8'),
              'doctype':'json',
              'xmlVersion':'1.8',
              'keyfrom':'fanyi.web',
              'ue':'UTF-8',
              'action':'FY_BY_CLICKBUTTON',
              'typoResult':'true'
          }
    
      r = requests.post(URL,headers=headers,data=data)
  • Response object

      >>> r
    <Response [200]>#200 is a status code that indicates the normal response of http requests
    >>> r.content.split()#split removes spaces
    [b'{"type":"ZH_CN2EN","errorCode":0,"elapsedTime":0,"translateResult":[[{"src":"\xe5\x8a\xa0\xe6\xb2\xb9","tgt":"Come', b'on"}]],"smartResult":{"type":1,"entries":["","refuel","cheer","play', b'up"]}}']
    >>>

8.JSON - JavaScript Object Markup

  • Examples of json data

      '{"type":"ZH_CN2EN",
      "errorCode":0,
      "elapsedTime":0,
      "translateResult":[[{"src":"\xe5\x8a\xa0\xe6\xb2\xb9","tgt":"Come', b'on"}]],
      "smartResult":{"type":1,"entries":["","refuel","cheer","play', b'up"]}}'
  • Convert json string to Python data

          import json
    • Direct Conversion String

        json.loads()
    • String in conversion file

        json.load()
  • Convert Python data into json strings

    • Converting Python data types to Json strings

        json.dumps()
    • Converting Python data types to files containing Json strings

        json.dump()

Build your own development environment in Linux: Take tornado and sqlalchemy as examples

  • 1. Create a separate project path

    xiaodong@Xiaodong-VMSrv:~/work/tornado_test/xiaodong1$

    xiaodong@Xiaodong-VMSrv:~$ mkdir work
    xiaodong@Xiaodong-VMSrv:~$ cd work/
    xiaodong@Xiaodong-VMSrv:~/work$ ls
    xiaodong@Xiaodong-VMSrv:~/work$ mkdir tornado_test
    xiaodong@Xiaodong-VMSrv:~/work$ ls
    tornado_test
    xiaodong@Xiaodong-VMSrv:~/work$ cd tornado_test/
    xiaodong@Xiaodong-VMSrv:~/work/tornado_test$ ls
    xiaodong@Xiaodong-VMSrv:~/work/tornado_test$ mkdir xiaodong1
    xiaodong@Xiaodong-VMSrv:~/work/tornado_test$ ls
    xiaodong1
  • 2. Install Virtualenv Library
    xiaodong@Xiaodong-VMSrv:~$ pip install -i https://pypi.douban.com/simple/ virtualenv
    Collecting virtualenv
    Downloading https://pypi.doubanio.com/packages/6f/..../virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
      100% |████████████████████████████████| 1.8MB 3.4MB/s 
    Installing collected packages: virtualenv
    Successfully installed virtualenv-15.1.0
  • 3. Create a virtual environment in this path

    xiaodong@Xiaodong-VMSrv:~/work/tornado_test/xiaodong1$ ls
    xiaodong@Xiaodong-VMSrv:~/work/tornado_test/xiaodong1$ virtualenv python2_tornado
    New python executable in /home/xiaodong/work/tornado_test/xiaodong1/python2_tornado/bin/python
    Installing setuptools, pip, wheel...done.
  • 5. Activating Virtual Environment

    xiaodong@XiaodongVMSrv:~/work/tornado_test/xiaodong1/python2_tornado$ source bin/activate
  • 4. Install tornado,sqlarchemy

    (python2_tornado) xiaodong@Xiaodong-VMSrv:~/work/tornado_test/xiaodong1/python2_tornado$ pip install tornado
    Collecting tornado
    Downloading tornado-4.5.1.tar.gz (483kB)
      100% |████████████████████████████████| 491kB 419kB/s 
    ......#Some content is omitted here
    Successfully installed backports-abc-0.5 certifi-2017.4.17 singledispatch-3.4.0.3 tornado-4.5.1
    (python2_tornado) xiaodong@Xiaodong-VMSrv:~/work/tornado_test/xiaodong1/python2_tornado$ pip install sqlalchemy
    Collecting sqlalchemy
    ......#Some content is omitted here
    Downloading SQLAlchemy-1.1.9.tar.gz (5.2MB)
      100% |████████████████████████████████| 5.2MB 47kB/s 
    ......
    Successfully built sqlalchemy
    Installing collected packages: sqlalchemy
    Successfully installed sqlalchemy-1.1.9

Posted by CaptianChaos on Mon, 07 Jan 2019 08:18:11 -0800