Python 基础记录

摘要

本文主要记录零碎python学习中遇到的一些知识点

pip基础

pip基础命令如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
install                     Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
inspect Inspect the python environment.
list List installed packages.
show Show information about installed packages.
check Verify installed packages have compatible dependencies.
config Manage local and global configuration.
search Search PyPI for packages.
cache Inspect and manage pip's wheel cache.
index Inspect information available from package indexes.
wheel Build wheels from your requirements.
hash Compute hashes of package archives.
completion A helper command used for command completion.
debug Show information useful for debugging.
help Show help for commands.

总之,对于pip命令有不了解的地方,建议通过运行pip help来查看。

可以通过pip install 批量安装package,命令如下:

1
pip install -r requirements.txt

requirements.txt文件格式:package_name==package_version,示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
astroid==2.0.4
atomicwrites==1.1.5
attrs==18.1.0
flake8==3.5.0
isort==4.3.4
lazy-object-proxy==1.3.1
mccabe==0.6.1
more-itertools==4.3.0
pluggy==0.7.1
py==1.5.4
pycodestyle==2.3.1
pyflakes==1.6.0
pylint==2.1.1
pytest==3.7.2
six==1.11.0
wrapt==1.10.11

Python错误

TypeError: ‘module’ object is not callable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Pepole.py
class Pepole:
name = 'Randy'
age = 18
gender = 'boy'

def __init(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender

def info():
print(self.name)

# 下面的代码尝试从别的文件中使用Pepole.py

import Pepole

if __name__ == "__main__":
peple = Pepole('Lucy', 18, 'Girl')
peple.info()

上面的代码运行时就会报TypeError: 'module' object is not callable错误,原因是,通过import People这种方式引入报,在调用内容是,需要加上相应module的前缀,即上面代码改成如下就可以正常运行了:

1
2
3
4
5
import Pepole

if __name__ == "__main__":
peple = People.Pepole('Lucy', 18, 'Girl')
peple.info()

或者

1
2
3
4
5
from People import Pepole

if __name__ == "__main__":
peple = Pepole('Lucy', 18, 'Girl')
peple.info()

这设计到Python 两种 module 导入方式:

  • import module,调用时需要加上module限定

  • from module import,调用时不需要加module限定

OSError: [Errno 9] Bad file descriptor

遇到这种错误的原因可能是多样的,在使用socket时,如果在调用socket.close

PyCharm编辑器相关

注意:当前使用的是PyCharm 2023.2.2 (Professional Edition)

Live Template

  • 代码模版使用,快捷键,cmd+,,然后搜索 Live Template,即可设置动态代码提示,如 输入main,就可以直接选择live template后就可以直接输出如下内容了:

    1
    if __name__ == "__main__":

Python packages

查看当前项目所用到的外部依赖包,具体图片如下:

Python packages

窗口包含如下信息:

  • 当前项目已经安装的包;

  • 点击具体的包,可以看到升级信息,以及包对应的文档

  • 上方的输入框可以关键字搜索PyPI 上已经存在的可用的包,十分方便;

编辑器显示空白

有时候需要查看文件中的空白(包括空格、空行、制表符等),这个时候可以通过Settings->Apperance->Show whitespaces,勾选即可显示

Python 中的类继承

假设有一个父类 Animal,子类 Dog 继承自 Animal:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Animal:
def __init__(self, name):
self.name = name

def speak(self):
print(f"{self.name} makes a sound")

class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name) # 调用父类的初始化方法
self.breed = breed

def speak(self):
print(f"{self.name} barks")

# 使用示例
dog = Dog("Buddy", "Golden Retriever")
dog.speak() # 输出: Buddy barks