从所有.py文件中生成模块列表并安装而无需指定模块版本
我有数百个python文件,我需要安装所有现有模块,我想我会创建一个文件,其中包含文件中导入的所有模块名称:
import os
def remove_duplicates():
lines = open('all_modules.txt', 'r').readlines()
lines_set = set(lines)
out = open('all_modules.txt', 'w')
for line in lines_set:
out.write(line)
def main():
fileDir = r"C:\Users\Computador\Desktop\tests vscode"
fileExt = r".py"
py_file_list = [_ for _ in os.listdir(fileDir) if _.endswith(fileExt)]
with open('all_modules.txt', 'w+', newline='', encoding='UTF-8') as f:
for py_file in py_file_list:
with open(py_file) as stacktest:
stacktest_list = [line.strip() for line in stacktest.readlines()]
for line in stacktest_list:
if 'import ' in line and "'" not in line:
after_import = line.split('import ',1)[1]
before_space = after_import.split(' ')[0]
before_comma = before_space.split(',')[0]
f.write(before_comma + '\n')
remove_duplicates()
if __name__ == '__main__':
main()
示例结果alll_modules.txt
:
pandas
time
dataframe_image
webbrowser
num2words
glob
Pool
schedule
move
csv
os
feedparser
但是,要使用unigess.txt
安装PIP安装,必须让我要安装的模块的版本,在这种情况下,计算机是完全干净的,我想要所有模块的最新版本。
有没有办法立即安装这些模块而无需手动编写?
I have hundreds of python files and I need to install all the existing modules, I thought I'd create a file with all the module names that are imported in the files:
import os
def remove_duplicates():
lines = open('all_modules.txt', 'r').readlines()
lines_set = set(lines)
out = open('all_modules.txt', 'w')
for line in lines_set:
out.write(line)
def main():
fileDir = r"C:\Users\Computador\Desktop\tests vscode"
fileExt = r".py"
py_file_list = [_ for _ in os.listdir(fileDir) if _.endswith(fileExt)]
with open('all_modules.txt', 'w+', newline='', encoding='UTF-8') as f:
for py_file in py_file_list:
with open(py_file) as stacktest:
stacktest_list = [line.strip() for line in stacktest.readlines()]
for line in stacktest_list:
if 'import ' in line and "'" not in line:
after_import = line.split('import ',1)[1]
before_space = after_import.split(' ')[0]
before_comma = before_space.split(',')[0]
f.write(before_comma + '\n')
remove_duplicates()
if __name__ == '__main__':
main()
Example result in all_modules.txt
:
pandas
time
dataframe_image
webbrowser
num2words
glob
Pool
schedule
move
csv
os
feedparser
But to install with pip install using requirements.txt
it is necessary to have the version of the module I want to install, in this case, the computer is completely clean and I want the latest version of all modules.
Is there a way to install these modules at once without having to write one by one manually?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
。您可以获取已生成的文件并运行
pip install -r all_modules.txt
pip将遍历文件中的每个名称,并尝试安装可用的最新版本(这可能不是与您的版本相同的版本最初编写代码时使用 - 事情可能不起作用,但至少会安装软件包)。
您可能会遇到的其他问题是:
import
语句中提取的,但是软件包的导入名称不一定与您需要pip的PYPI中的包装名称匹配安装That's not true. You can take the file you have generated and run
pip install -r all_modules.txt
Pip will go through each name in the file and attempt to install the latest version available (that might not be the same version you used when writing the code originally - things might not work, but it will at least install the package).
The other problems you will likely encounter are:
import
statements, but the import name of a package does not necessarily match the name of the package in PyPI that you need to pip install