@@ -113,46 +113,45 @@ description: 九月生活与职业成长随想
113113
114114## 将Cursor添加到右键菜单
115115
116- 通过脚本读取当前用户名,并在桌面生成名为` AddCursorToContextMenu.reg ` 的注册表文件。如权限不足, 可在桌面找到该文件,以管理员身份运行。
116+ 通过脚本读取当前用户名,并在桌面生成名为` AddCursorToContextMenu.reg ` 的注册表文件。可在桌面找到该文件,以管理员身份运行。
117117
118118``` python
119119import os
120- import subprocess
121120
122- # 获取当前用户名
123121username = os.getenv(' USERNAME' )
124-
125- # 获取当前用户的桌面路径
126- desktop_path = os.path.join(os.path.expanduser(" ~" ), ' Desktop' )
127-
128- # 构造 Cursor.exe 的路径
129122cursor_path = fr " C: \\Users \\{ username} \\AppData \\Local \\Programs \\Cursor \\Cursor.exe "
130123
131- # 生成注册表内容
124+
132125reg_content = f """ Windows Registry Editor Version 5.00
133126
134127[HKEY_CLASSES_ROOT \\ * \\ shell \\ Open with Cursor]
135128@="Open with Cursor"
136- "Icon"=" \" { cursor_path} \" "
129+ "Icon"=" { cursor_path} "
130+
137131[HKEY_CLASSES_ROOT \\ * \\ shell \\ Open with Cursor \\ command]
138- @=" \" { cursor_path} \" \" %1 \" "
132+ @=" \\ " { cursor_path} \\ " \\ "%1 \ \""
139133
140134[HKEY_CLASSES_ROOT \\ Directory \\ shell \\ Open with Cursor]
141135@="Open with Cursor"
142- "Icon"=" \" { cursor_path} \" "
136+ "Icon"=" { cursor_path} "
137+
143138[HKEY_CLASSES_ROOT \\ Directory \\ shell \\ Open with Cursor \\ command]
144- @=" \" { cursor_path} \" \" %1 \" "
139+ @=" \\ " { cursor_path} \\ " \\ "%1 \ \""
145140
146141[HKEY_CLASSES_ROOT \\ Directory \\ Background \\ shell \\ Open with Cursor]
147- @="Open with Cursor当前文件夹"
148- "Icon"=" \" { cursor_path} \" "
142+ @="Open with Cursor current folder"
143+ "Icon"=" { cursor_path} "
144+
149145[HKEY_CLASSES_ROOT \\ Directory \\ Background \\ shell \\ Open with Cursor \\ command]
150- @=" \" { cursor_path} \" \" %V \" "
146+ @=" \\ " { cursor_path} \\ " \\ "%V \ \""
151147
152- [HKEY_CURRENT_USER \\ Software \\ Classes \\ CLSID \\{{ 86ca1aa0-34aa-4e8b-a509-50c905bae2a2 }}\\ InprocServer32]
153- @=""
154148"""
155149
150+ # 获取当前用户的桌面路径
151+ desktop_path = os.path.join(os.path.expanduser(" ~" ), ' Desktop' )
152+
153+ # 构造 Cursor.exe 的路径
154+
156155# 生成 .reg 文件的完整路径,保存在桌面
157156reg_file_path = os.path.join(desktop_path, " AddCursorToContextMenu.reg" )
158157
@@ -162,11 +161,71 @@ with open(reg_file_path, "w", encoding="utf-8") as file:
162161
163162print (f " .reg 文件已保存到: { reg_file_path} " )
164163
165- # 执行 .reg 文件来导入注册表
166- try :
167- subprocess.run([' regedit.exe' , ' /s' , reg_file_path], check = True )
168- print (" 注册表文件已成功导入。" )
169- except subprocess.CalledProcessError as e:
170- print (f " 导入注册表文件时出错: { e} " )
164+ ```
165+
166+ ## 导入模块
167+
168+ 现在大家都知道可以通过` pip install -r requirements.txt ` 安装模块,但是有些模块可能安装失败,需要人手动再次安装。不能做到自动化。这里提供一个脚本,可以自动安装失败的模块2次,并保存到` failed_requirements.txt ` 。
169+
170+ ``` python showLineNumbers
171+ import subprocess
172+
173+ def install_requirements (requirements_file , failed_file ):
174+ failed_packages = []
175+
176+ # 打开 requirements.txt 并读取模块名称
177+ with open (requirements_file, ' r' ) as file :
178+ packages = file .readlines()
179+
180+ # 遍历每个包名并尝试安装
181+ for package in packages:
182+ package = package.strip()
183+ if package: # 跳过空行
184+ print (f " 正在安装: { package} " )
185+ try :
186+ # 使用 subprocess 执行 pip 安装命令
187+ result = subprocess.run(
188+ [' pip' , ' install' , package],
189+ stdout = subprocess.PIPE ,
190+ stderr = subprocess.PIPE ,
191+ text = True
192+ )
193+
194+ # 检查安装结果,如果返回码不为0则表示安装失败
195+ if result.returncode != 0 :
196+ print (f " 安装失败: { package} " )
197+ failed_packages.append(package)
198+
199+ except Exception as e:
200+ print (f " 发生错误: { e} " )
201+ failed_packages.append(package)
202+
203+ # 将安装失败的模块写入 failed_requirements.txt
204+ if failed_packages:
205+ with open (failed_file, ' w' ) as f:
206+ f.write(" \n " .join(failed_packages))
207+ print (f " 安装失败的模块已保存到 { failed_file} " )
208+ else :
209+ print (" 所有模块均安装成功" )
210+ def remove_versions (requirements_file , output_file ):
211+ with open (requirements_file, ' r' ) as file :
212+ lines = file .readlines()
213+
214+ cleaned_lines = []
215+ for line in lines:
216+ # 去除版本号部分(==后面的内容)
217+ package = line.split(' ==' )[0 ].strip()
218+ if package: # 跳过空行
219+ cleaned_lines.append(package)
220+
221+ # 将去掉版本号的包名写入新的文件
222+ with open (output_file, ' w' ) as output:
223+ output.write(" \n " .join(cleaned_lines))
224+
225+ print (f " 已将无版本号的依赖项写入 { output_file} " )
226+
227+ for i in range (2 ):
228+ install_requirements(' requirements.txt' , ' failed_requirements.txt' )
229+ remove_versions(' failed_requirements.txt' , ' requirements.txt' )
171230
172231```
0 commit comments