python模块导入和帮助
Posted 张贺贺
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python模块导入和帮助相关的知识,希望对你有一定的参考价值。
模块导入和帮助
下载和导入
python‘s standard library is amazing
python模块:
模块就是保存在一个单独文件当中的一段代码。
函数是一个积木,多个积木组成起来就是一个模块,模块在python当中的具体体现其实就是一个文件而已,我们可以导入模块,进而使用模块当中的函数。
python当中内置了一些模块,但是这些模块需要导入才能使用,为什么python不能直接替我们进行导入,我们直接使用呢?其实这是为了让python不臃肿,这种方式被很多程序借鉴,比如obsidian这个软件,默认安装后只有基本的功能,你想使用一些高级功能,需要自定义安装或打开。
最著名一个库网站:pypi.org,如下所示:
Welcome to Paramiko! — Paramiko documentation
# 查看安装了哪些库文件
C:\\Users\\zhanghe>pip list
Package Version
------------ --------
bcrypt 4.0.1
cffi 1.15.1
cryptography 39.0.1
nmap 0.0.1
paramiko 3.0.0
pip 22.3.1
pycparser 2.21
pygame 2.1.3
PyNaCl 1.5.0
setuptools 65.5.0
you-get 0.4.1650
[notice] A new release of pip available: 22.3.1 -> 23.0.1
[notice] To update, run: python.exe -m pip install --upgrade pip
# 通过pip安装模块 (pip自己了是一个模块)
# 换源,国外的有点慢,可以用一下国内的,比如阿里的
方法自己去阿里镜像源上看
# 安装
apt update ; apt -y install python-pip3
# pip升级
pip3 install --upgrade pip
Downloading pip-23.0.1-py3-none-any.whl (2.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.1/2.1 MB 691.0 kB/s eta 0:00:00
# 列出pip正在使用的配置文件
pip config list -v
root@DESKTOP-RAG9L0F:~# pip config list -v
For variant \'global\', will try loading \'/etc/xdg/pip/pip.conf\'
For variant \'global\', will try loading \'/etc/pip.conf\'
For variant \'user\', will try loading \'/root/.pip/pip.conf\'
For variant \'user\', will try loading \'/root/.config/pip/pip.conf\'
For variant \'site\', will try loading \'/usr/pip.conf\'
# 查看已经安装好的模块
python3 -m pip list # 或 pip3 list
# 安装和升级SNMP模块
pip3 install pysnmp
pip3 install --upgrade pysnmp
root@DESKTOP-RAG9L0F:~# pip3 install paramiko
root@DESKTOP-RAG9L0F:~# python3 -m pip list | grep -i paramiko
paramiko 3.1.0
# 默认的安装位置
/usr/lib/python3/dist-packages
root@DESKTOP-RAG9L0F:~# which python3
/usr/bin/python3
# 查看模块的详细信息
pip3 show pysnmap
root@DESKTOP-RAG9L0F:/etc# pip3 show paramiko
Name: paramiko
Version: 3.1.0
Summary: SSH2 protocol library
Home-page: https://paramiko.org
Author: Jeff Forcier
Author-email: jeff@bitprophet.org
License: LGPL
Location: /usr/local/lib/python3.10/dist-packages
Requires: bcrypt, cryptography, pynacl
Required-by:
# 搜索模块要安装搜索模块
pip list | grep search
pip3 install pip-search
pip_search ftp
# 安装模块的第一种方式
#安装nmap模块和nmap模块的使用方法请参考 https://xael.org/pages/python-nmap-en.html
┌──(root㉿kali)-[~/python-nmap-0.7.1]
└─# pip3 list | grep -i nmap
python-nmap 0.7.1
┌──(root㉿kali)-[~/python-nmap-0.7.1]
└─# pip3 uninstall python-nmap
┌──(root㉿kali)-[~]
└─# wget https://xael.org/pages/python-nmap-0.7.1.tar.gz
┌──(root㉿kali)-[~]
└─# tar -zxvf python-nmap-0.7.1.tar.gz
┌──(root㉿kali)-[~]
└─# cd python-nmap-0.7.1
┌──(root㉿kali)-[~/python-nmap-0.7.1]
└─# python setup.py install
root㉿kali)-[~/python-nmap-0.7.1]
└─# pip3 list | grep -i nmap
python-nmap 0.7.1
---------------------------------
# 安装模块的第二种方式
┌──(root㉿kali)-[~]
└─# python3 -m pip install python-nmap
┌──(root㉿kali)-[~]
└─# python3
Python 3.10.8 (main, Nov 4 2022, 09:21:25) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import nmap # 引入模块
>>> nm = nmap.PortScanner() # 将模块赋值给一个变量nm
>>> nm.scan(\'127.0.0.1\',\'22-100\') # 通过nm调用子命令scan
\'nmap\': \'command_line\': \'nmap -oX - -p 22-100 -sV 127.0.0.1\', \'scaninfo\': \'tcp\': \'method\': \'syn\', \'services\': \'22-100\', \'scanstats\': \'timestr\': \'Thu Feb 23 05:54:59 2023\', \'elapsed\': \'0.55\', \'uphosts\': \'1\', \'downhosts\': \'0\', \'totalhosts\': \'1\', \'scan\': \'127.0.0.1\': \'hostnames\': [\'name\': \'localhost\', \'type\': \'PTR\'], \'addresses\': \'ipv4\': \'127.0.0.1\', \'vendor\': , \'status\': \'state\': \'up\', \'reason\': \'localhost-response\'
# 看具体是通过哪个命令实现的,上方当中也有显示
>>> nm.command_line()
\'nmap -oX - -p 22-100 -sV 127.0.0.1\'
----
┌──(root㉿kali)-[~]
└─# cat /tmp/namp.py
import nmap
nm = nmap.PortScanner()
SCAN=nm.scan(\'127.0.0.1\',\'22-100\')
COMMD=nm.command_line()
print(SCAN,COMMD)
模块帮助
# 多个模块之前以逗号分隔即可,如下所示
import sys,os
# 导入sys库,此库是主要用来向python脚本当中传递命令行参数时使用的,也可以用做代码审计
>>> import sys
通过dir函数查看sys库有哪些方法,这种方法又被称为内省.
>>> import sys
>>> dir(sys)
[\'__breakpointhook__\', \'__displayhook__\', \'__doc__\', …………
# 通过dir进一步查看(库+方法),这个方法又支持哪些具体方法
>>> import paramiko
>>> dir(paramiko)
\'SSHConfig\', \'SSHConfigDict\', \'SSHException\' ……
>>> dir(paramiko.SSHConfig)
[\'SETTINGS_REGEX\', \'TOKENS_BY_CONFIG_KEY\', \'__class__\', \'__delattr__\', \'__dict__\', ……
也可以去pypi.org网站上查看库支持的方法
常用帮助
HELP
HELP两种使用方式:
- 交互式的
- 命令行
# 交互式,如下所示,告诉你print是一个内置函数
>>> help()
>>> print
<built-in function print>
# 我们查看一下random,它告诉我们random是一个模块
help> random
Help on module random:
NAME
random - Random variable generators.
MODULE REFERENCE
# 列出所有已经安装的模块
help> modules
# 列出所有的keywords
help> keywords
Here is a list of the Python keywords. Enter any keyword to get more help.
False class from or
None continue global pass
True def if raise
and del import return
as elif in try
assert else is while
async except lambda with
await finally nonlocal yield
break for not
# 显示所有的运算符
help> symbols
Here is a list of the punctuation symbols which Python assigns special meaning
to. Enter any symbol to get more help.
!= + <= __
" += <> `
""" , == b"
% - > b\'
%= -= >= f"
& . >> f\'
&= ... >>= j
\' / @ r"
\'\'\' // J r\'
( //= [ u"
) /= \\ u\'
* : ] |
** < ^ |=
**= << ^= ~
*= <<= _
# 查询这个符号是什么意思
help> !=
Operator precedence
*******************
The following table summarizes the operator precedence in Python, from
highest precedence (most binding) to lowest precedence (least
binding). Operators in the same box have the same precedence. Unless
the syntax is explicitly given, operators are binary. Operators in
the same box group left to right (except for exponentiation and
conditional expressions, which group from right to left).
Note that comparisons, membership tests, and identity tests, all have
the same precedence and have a left-to-right chaining feature as
described in the Comparisons section.
>>> help(os)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name \'os\' is not defined
>>> import os
>>> help(os)
Help on module os:
NAME
os - OS routines for NT or Posix depending on what system we\'re on.
>>> help(\'os\')
>>> help(\'paramiko\')
>>> help(\'nmap\')
# 其实是对pydoc的一个封装
>>> help(help)
Help on _Helper in module _sitebuiltins object:
class _Helper(builtins.object)
| Define the builtin \'help\'.
|
| This is a wrapper around pydoc.help that provides a helpful message
| when \'help\' is typed at the Python interactive prompt.
# 生成doc文档的
>>> help(\'pydoc\')
Help on module pydoc:
NAME
pydoc - Generate Python documentation in HTML or text for interactive use.
文档
用途 | 网址 |
---|---|
查看内存 | https://pythontutor.com/ |
中文官方文档 | https://docs.python.org/zh-cn/3/ |
英文官方文档 | https://docs.python.org/3/ |
在安装完python之后,一般也会附带一个HTML格式的python文档,如下所示:
以上是关于python模块导入和帮助的主要内容,如果未能解决你的问题,请参考以下文章