[英]How can I make my python program install modules automatically on new computers?
So I've written a program in python that I want to use on different computers, but to do this I need to install modules by opening up the command prompt and using "pip install ".所以我用 python 编写了一个程序,我想在不同的计算机上使用它,但要做到这一点,我需要通过打开命令提示符并使用“pip install”来安装模块。
Is there a way to do this in the program without having to open the command prompt?有没有办法在程序中做到这一点而不必打开命令提示符?
Yes, you can use the built-in pip
module, which allows you to install python packages within your code.是的,您可以使用内置的
pip
模块,它允许您在代码中安装 python 包。
eg例如
import pip
pip.main(["install", "pygame"])
in order to check for your package, you could use a try-except and check for an ImportError为了检查您的包,您可以使用 try-except 并检查 ImportError
I suspect if there is any way but here's the trick:我怀疑是否有任何办法,但这是诀窍:
import os
os.system('pip install --user <module>')
And if you have many different packages to install, you may create requirements.txt
file and just do pip install -r requirements.txt
如果你有很多不同的包要安装,你可以创建
requirements.txt
文件,然后执行pip install -r requirements.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.