1、pycharm安装与环境配置
https://www.runoob.com/w3cnote/pycharm-windows-install.html
2、模块中的坑
import cv2
直接安装cv2模块
因为cv2是包含在opencv-python模块中的,安装opencv-python模块后就可以正常使用cv2
下载速度慢
使用国内镜像网站
File→Setting→Project Interpreter
在添加库的界面点击Manage Repositories
添加国内镜像网站例如:http://pypi.doubanio.com/simple/
python3自带json模块,已有模块无法使用,与python file重名
3、虚拟环境的坑
正常运行没问题时
用命令行编译出现问题
修改环境变量,在path中加入python编译器路径
继续编译,出现同样错误
pycharm自动加载的环境为虚拟环境
虚拟环境可以在系统python编译器中避免包的混乱和版本的冲突
在真实主机python里安装的东西,在虚拟环境中是没有的
在创建虚拟环境时勾选
Inherit global site-packages 继承源解释器环境中安装的包
Make available to all projects 用到所有项目
4、
if __name__='__main__':
python文件的两种使用方法:
作为脚本直接运行
import到其他python脚本中被调用
#test.py
print ("I'm the first.")
print(__name__)
if __name__=="__main__":
print ("I'm the second.")
#test.py 运行结果
I'm the first.
__main__
I'm the second.
#a.py
import test
print(__name__)
#a.py 运行结果
I'm the first.
test
__main__
在if name='main’情况下
代码只有在第一种情况下才会被执行
而在import到其他脚本中不会
5、单下划线和双下划线
python中并不存在真正的私有方法,为了实现私有方法在函数前加"_"意味着该方法不应该被调用
#_and__
class A:
def _method(self):
print('class A ')
def method(self):
return self._method()
a = A()
a.method()
a._method()
#运行结果
class A
class A
实际上在函数前加"_"并不影响其调用,只是约定上不应该被调用
class A:
def __method(self):
print('class A')
def method(self):
return self.__method()
class B(A):
def __method(self):
print('calss B')
a=A()
b=B()
a.method()
b.method()
a.__method()
__不能直接被调用,防止函数被子类覆盖
参考:
https://blog.csdn.net/helloxiaozhe/article/details/80534894 单下划线和双下划线
https://blog.csdn.net/zhusongziye/article/details/79505803 if name=‘main’