Python学习之==>模块结构调整

一、为什么要进行模块结构调整

  当一个脚本中有大量的配置、方法及接口时,脚本显得十分臃肿,可读性很差。为了提高代码的易读性,可以将一个繁杂的脚本根据不同的功能放在不同的目录下分类管理,这整个过程叫做模块结构调整。完成模块结构调整后的代码可读性强,修改起来也更简单,调整后的代码在任何服务器、操作系统都可以执行,不会报错。

二、模块结构调整实例

注册接口

  (1)注册信息存入redis数据库

  (2)redis的key用string类型的,密码使用md5加密

  (3)注册时需要校验用户是否已经被注册

登录接口

  (1)登录成功之后,返回seesionid、用户登录时间

  (2)如果用户是第一次登录或者seesion已失效,需要在redis中存入session。sessionid计算规则:(用户名+当前的时间戳 )md5加密,失效时间是60分钟

  (3)存入redis的session格式如下:

      Key:session:niuniu

      Value:{"seessionid":a5dfcb047721e02a6f8bff779c815165,"login_time":201805051820}

  (4)如果已经登录且未失效,则直接返回session和用户登录时间

1、创建API主目录及其下面各子目录

 

  (1)bin:存放启动服务的可执行脚本

  (2)config:存放配置文件,如mysql, redis的配置信息

  (3)data:存放sql语句,创建表格或者类似的东西

  (4)lib:存放开发使用到的函数和开发好的接口

  (5)logs:存放日志文件

  (6)readme:简要介绍程序运行及参数修改的说明文档

2、将主目录(my_api)加入环境变量

  各个模块(文件)放在不同的目录下相互引用,引用自己写的的Python文件做为模块时需要将路径加入环境变量,否则会报错。选中主目录右键→Mark Directory as→Sources Root后,Pycharm将整个api文件夹统一加入环境变量。但这种方式只对这台机器有效果,如果换一台机器或者把这个my_api项目放到服务器上执行,这种方式是不行的。所以一般使用这种方式加入环境变量是便于我们在Pycharm上编写和调试代码,另外一种加入环境变量的方式会在后面讲到。

3、将不同功能的Python文件放入对应的目录

a、将配置文件setting.py文件放入config目录下,存入要用到的配置参数,如:REDIS、MySQL配置信息以及服务端口等。

 1 # redis配置信息
 2 REDIS_INFO = {
 3     'host':'127.0.0.1',#IP
 4     'password':'',     #密码
 5     'db':1,            #数据库
 6     'port':6379,       #端口号
 7 }
 8 
 9 # mysql配置信息
10 MYSQL_INFO = {
11     'host':'192.168.1.112',#IP
12     'user':'test',         #用户名
13     'passwd':'123456',     #密码
14     'db':'test',           #数据库
15     'port':3306,           #端口号
16     'charset':'utf8',      #字符集
17 }
18 
19 # 服务端口号
20 SERVER_PORT = 8888

b、tools.py文件放入lib目录下,存放操作REDIS、MySQL以及MD5加密函数

 1 from config.setting import MYSQL_INFO,REDIS_INFO  # 从config.setting文件导入REDIS和MYSQL连接参数
 2 # 连接mysql
 3 def my_db(sql):
 4     import pymysql
 5     conn = pymysql.connect(**MYSQL_INFO)
 6     cur = conn.cursor()
 7     cur.execute(sql)
 8     if sql.strip().split()[0].upper() == 'SELECT':
 9         res = cur.fetchall()
10     else:
11         conn.commit()
12         res = 'OK'
13     cur.close()
14     conn.close()
15     return res
16 
17 # 操作redis
18 def op_redis(k,v=None,time=None):
19     import redis
20     r = redis.Redis(**REDIS_INFO)
21     if v:
22         r.set(k,v,time)
23         res = 'ok'
24     else:
25         res = r.get(k)
26         if res:
27             res = res.decode()
28     return res
29 
30 # 加密函数
31 def my_md5(str):
32     import hashlib
33     new_str = str.encode()
34     m = hashlib.md5()
35     m.update(new_str)
36     return m.hexdigest()

c、interface.py文件放入lib目录下,存放注册及登录接口

 1 import flask,json
 2 server = flask.Flask(__name__)  #把当前这个python文件当作一个服务
 3 from lib.tools import my_md5,op_redis
 4 @server.route('/reg',methods=['post'])  #'reg'是接口路径,如果methods不写,则默认get请求
 5 def reg():
 6     username =flask.request.values.get('username')
 7     pwd = flask.request.values.get('pwd')
 8     if username and pwd:
 9         key = 'user:%s'%username
10         if op_redis(key): # 判断用户是否存在
11             res = {'msg':'用户已存在','msg_code':2001}
12         else:      # 不存在则将用户名和密码写入redis数据库
13             value = my_md5(pwd)
14             op_redis(key,value)
15             res = {'msg':'注册成功','msg_code':0000}
16     else:
17         res = {'msg':'必填字段未填,请查看接口文档!','msg_code':1001}
18     return json.dumps(res,ensure_ascii=False)
19 
20 @server.route('/login',methods= ['post'])
21 def login():
22     import time
23     username =flask.request.values.get('username')
24     pwd = flask.request.values.get('pwd')
25     if username and pwd:
26         user_key = 'user:%s'%username # 存放用户信息的key
27         passed = op_redis(user_key)
28         session_key = 'session:%s'%username # 存放登录session的key
29         session = op_redis(session_key)
30         if passed:
31             if my_md5(pwd) == passed:
32                 if session: # 判断之前有没有登录过
33                     res = json.loads(session)
34                 else:       # 之前没有登录过则将session写入redis数据库
35                     login_time = str(time.strftime('%Y%m%d%H%M%S'))
36                     sessionid = my_md5(username + str(time.time())) # 计算sessionid
37                     res = {'sessionid': sessionid, 'login_time': login_time} # 构造好字典
38                     op_redis(session_key,json.dumps(res),600) # 将session存入redis并设置失效时间
39             else:
40                 res = {'msg':'密码错误','msg_code':2001}
41         else:
42             res = {'msg':'用户不存在','msg_code':2001}
43     else:
44         res = {'msg':'用户名或密码必填','msg_code':1001}
45     return json.dumps(res,ensure_ascii=False)

d、start.py文件放入bin目录下,用来启动服务

1 # 手工加环境变量,这样项目在任何环境任何系统中执行都不会报错
2 import sys,os
3 BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))  # 获取到当前程序的主目录
4 sys.path.insert(0,BASE_PATH)  # 将程序主目录加入python环境变量
5 
6 from lib.interface import server
7 from config.setting import SERVER_PORT
8 server.run(host='0.0.0.0',port=SERVER_PORT,debug=True)

至此,模块结构调整完成,运行start.py即可启动程序。

转载于:https://www.cnblogs.com/L-Test/p/9228439.html

终端运行python文件命令ERROR: Ignored the following versions that require a different python version: 1.11.0 Requires-Python <3.13,>=3.9; 1.11.0rc1 Requires-Python <3.13,>=3.9; 1.11.0rc2 Requires-Python <3.13,>=3.9; 1.11.1 Requires-Python <3.13,>=3.9; 1.11.2 Requires-Python <3.13,>=3.9; 1.11.3 Requires-Python <3.13,>=3.9; 1.11.4 Requires-Python >=3.9; 1.12.0 Requires-Python >=3.9; 1.12.0rc1 Requires-Python >=3.9; 1.12.0rc2 Requires-Python >=3.9; 1.13.0 Requires-Python >=3.9; 1.13.0rc1 Requires-Python >=3.9; 1.13.1 Requires-Python >=3.9; 1.14.0 Requires-Python >=3.10; 1.14.0rc1 Requires-Python >=3.10; 1.14.0rc2 Requires-Python >=3.10; 1.14.1 Requires-Python >=3.10; 1.15.0 Requires-Python >=3.10; 1.15.0rc1 Requires-Python >=3.10; 1.15.0rc2 Requires-Python >=3.10; 1.15.1 Requires-Python >=3.10; 1.15.2 Requires-Python >=3.10; 1.15.3 Requires-Python >=3.10; 1.25.0 Requires-Python >=3.9; 1.25.1 Requires-Python >=3.9; 1.25.2 Requires-Python >=3.9; 1.26.0 Requires-Python <3.13,>=3.9; 1.26.1 Requires-Python <3.13,>=3.9; 1.26.2 Requires-Python >=3.9; 1.26.3 Requires-Python >=3.9; 1.26.4 Requires-Python >=3.9; 11.0.0 Requires-Python >=3.9; 11.1.0 Requires-Python >=3.9; 11.2.1 Requires-Python >=3.9; 2.0.0 Requires-Python >=3.9; 2.0.1 Requires-Python >=3.9; 2.0.2 Requires-Python >=3.9; 2.1.0 Requires-Python >=3.10; 2.1.0rc1 Requires-Python >=3.10; 2.1.1 Requires-Python >=3.10; 2.1.2 Requires-Python >=3.10; 2.1.3 Requires-Python >=3.10; 2.2.0 Requires-Python >=3.10; 2.2.0rc1 Requires-Python >=3.10; 2.2.1 Requires-Python >=3.10; 2.2.2 Requires-Python >=3.10; 2.2.3 Requires-Python >=3.10; 2.2.4 Requires-Python >=3.10; 2.2.5 Requires-Python >=3.10; 3.10.0 Requires-Python >=3.10; 3.10.0rc1 Requires-Python >=3.10; 3.10.1 Requires-Python >=3.10; 3.10.3 Requires-Python >=3.10; 3.8.0 Requires-Python >=3.9; 3.8.0rc1 Requires-Python >=3.9; 3.8.1 Requires-Python >=3.9; 3.8.2 Requires-Python >=3.9; 3.8.3 Requires-Python >=3.9; 3.8.4 Requires-Python >=3.9; 3.9.0 Requires-Python >=3.9; 3.9.0rc2 Requires-Python >=3.9; 3.9.1 Requires-Python >=3.9; 3.9.1.post1 Requires-Python >=3.9; 3.9.2 Requires-Python >=3.9; 3.9.3 Requires-Python >=3.9; 3.9.4 Requires-Python >=3.9; 8.13.1 Requires-Python >=3.9; 8.13.2 Requires-Python >=3.9; 8.14.0 Requires-Python >=3.9; 8.15.0 Requires-Python >=3.9; 8.16.0 Requires-Python >=3.9; 8.16.1 Requires-Python >=3.9; 8.17.0 Requires-Python >=3.9; 8.17.1 Requires-Python >=3.9; 8.17.2 Requires-Python >=3.9; 8.18.0 Requires-Python >=3.9; 8.18.1 Requires-Python >=3.9; 8.19.0 Requires-Python >=3.10; 8.20.0 Requires-Python >=3.10; 8.21.0 Requires-Python >=3.10; 8.22.0 Requires-Python >=3.10; 8.22.1 Requires-Python >=3.10; 8.22.2 Requires-Python >=3.10; 8.23.0 Requires-Python >=3.10; 8.24.0 Requires-Python >=3.10; 8.25.0 Requires-Python >=3.10; 8.26.0 Requires-Python >=3.10; 8.27.0 Requires-Python >=3.10; 8.28.0 Requires-Python >=3.10; 8.29.0 Requires-Python >=3.10; 8.30.0 Requires-Python >=3.10; 8.31.0 Requires-Python >=3.10; 8.32.0 Requires-Python >=3.10; 8.33.0 Requires-Python >=3.10; 8.34.0 Requires-Python >=3.10; 8.35.0 Requires-Python >=3.10; 8.36.0 Requires-Python >=3.10; 9.0.0 Requires-Python >=3.11; 9.0.0b1 Requires-Python >=3.11; 9.0.0b2 Requires-Python >=3.11; 9.0.1 Requires-Python >=3.11; 9.0.2 Requires-Python >=3.11; 9.1.0 Requires-Python >=3.11; 9.2.0 Requires-Python >=3.11 ERROR: Could not find a version that satisfies the requirement thop==0.1.1 (from versions: 0.0.4.post2006032126, 0.0.4.post2006111041, 0.0.4.post2007140009, 0.0.4.post2009041907, 0.0.4.post2009191310, 0.0.4.post2108202217, 0.0.4.post2109101449, 0.0.4.post2109101452, 0.0.4.post2110061704, 0.0.5.post2110061705, 0.0.5.post2206100352, 0.0.5.post2206102148, 0.0.22, 0.0.22.post1906241147, 0.0.22.post421392844089, 0.0.23.post1906241153, 0.0.23.post1906241219, 0.0.23.post1906241402, 0.0.23.post1907011323, 0.0.23.post1907171426, 0.0.23.post1907171430, 0.0.23.post1908021457, 0.0.23.post1908021517, 0.0.23.post1908021522, 0.0.23.post1908061459, 0.0.30.post190806, 0.0.30.post1908061459, 0.0.30.post1908061501, 0.0.30.post1908281655, 0.0.30.post1908282105, 0.0.30.post1908282106, 0.0.30.post1908282117, 0.0.30.post1908302010, 0.0.31.post1909021322, 0.0.31.post1909230639, 0.0.31.post1910280903, 0.0.31.post1912272122, 0.0.31.post2001170342, 0.0.31.post2004070130, 0.0.31.post2004101309, 0.0.31.post2005141432, 0.0.31.post2005141830, 0.0.31.post2005241907, 0.1.0.post2206102148, 0.1.0.post2207010342, 0.1.1.post2207130030, 0.1.1.post2209072238)
最新发布
05-15
<think>嗯,用户的问题是关于解决thop库与Python版本不兼容的问题。首先,我需要回忆一下thop库的官方文档或者常见的使用情况。我记得thop是一个用来计算模型FLOPs和参数的库,通常和PyTorch一起使用。用户可能在使用时遇到了Python版本不符合的情况,导致安装失败或者运行错误。 接下来,用户想知道thop库的具体Python版本要求,以及哪些版本是不兼容的。这时候,我需要先确认thop库的兼容性信息。根据之前的经验,thop库通常支持Python 3.6及以上版本,但最好查看官方资料。比如,查看PyPI页面或者GitHub仓库的说明。例如,在PyPI上,thop的页面可能会列出支持的Python版本。 然后,用户需要不兼容版本的列表。可能有些Python版本如2.7或3.5以下已经不被支持,因为很多现代库已经放弃了对这些旧版本的支持。不过需要具体信息,可能需要查阅相关资源或者社区讨论。比如,GitHub的issue部分可能会有用户报告在特定Python版本下的问题。 解决方案部分,用户可能需要升级Python版本,或者使用虚拟环境来管理不同的Python版本。此外,可能thop有替代库,比如torchinfo,或者手动计算FLOPs的方法。如果用户的项目必须使用旧版Python,可能需要寻找旧版的thop,或者从源码安装并调整兼容性。 还要考虑用户的环境配置,比如是否正确安装了PyTorch,CUDA等依赖,因为这些也可能影响thop的运行。例如,引用[4]提到CUDA的问题,虽然主要错误不同,但环境配置是否正确也是关键。同时,引用[3]提到安装thop的命令,可能需要确认用户是否正确安装了thop。 用户可能的问题还包括如何检查当前Python版本,如何创建虚拟环境,或者如何降级thop版本。需要一步步引导用户操作,确保解决方案可行。此外,用户可能没有意识到他们使用的Python版本过旧,需要提示他们检查版本,并建议使用较新的版本。 最后,要确保回答结构清晰,分点列出要求、不兼容版本和解决方案,并给出验证步骤,帮助用户确认问题是否解决。同时,生成的相关问题要围绕Python版本管理、替代方案和常见错误处理,这样用户可以进一步解决可能遇到的问题。</think>### thop库的Python版本兼容性问题解决方案 #### 一、thop库的Python版本要求 1. **官方要求**: thop库(全称`torchprofile`)官方推荐使用 **Python 3.6及以上版本**。其最新版本(截至2023年)依赖现代Python语法和PyTorch生态[^3]。 2. **不兼容版本列表**: - Python 2.x系列(如2.7):因语法差异和EOL(终止支持)完全不兼容。 - Python 3.0-3.5:缺少`f-string`、`typing`模块等特性,可能导致安装或运行错误。 --- #### 二、解决方案 1. **升级Python版本** - 建议升级至Python 3.8+(兼顾稳定性和兼容性): ```bash # 使用conda conda install python=3.8 # 或使用pyenv(Linux/macOS) pyenv install 3.8.12 ``` 2. **使用虚拟环境隔离版本** 若系统需保留旧版本Python,可通过虚拟环境管理: ```bash # 创建Python 3.8虚拟环境 python3.8 -m venv thop_env source thop_env/bin/activate # Linux/macOS thop_env\Scripts\activate # Windows pip install thop ``` 3. **降级thop版本** 尝试安装旧版thop(部分旧版本支持低Python版本): ```bash pip install thop==0.0.31 # 测试兼容性 ``` 4. **替代方案** - **torchinfo**:支持Python 3.7+,功能类似: ```bash pip install torchinfo ``` - **手动计算FLOPs**:通过PyTorch Hook实现自定义统计[^3]。 --- #### 三、验证安装 ```python import torch from thop import profile model = torch.nn.Linear(10, 5) input = torch.randn(1, 10) flops, params = profile(model, inputs=(input,)) print(f"FLOPs: {flops}, Params: {params}") ``` 若输出结果无报错,则表明兼容性问题已解决[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值