简体   繁体   English

如何让我的 python 程序在新计算机上自动安装模块?

[英]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.

相关问题 如何使我的GUI程序(tkinter)在没有安装python的计算机上工作,从而不会显示控制台窗口? - How can I make my GUI program(tkinter) work on computers with no python installed such that no console window appears? 如何使我的 Python 程序提供的功能可用于在同一台计算机或其他计算机上运行的其他语言编写的程序? - How can I make the functions provided by my Python program available to programs written in other languages running on the same or other computers? 如果我使用pip安装模块,如何确保其他人可以在不安装该模块的情况下运行我的程序? - If I install modules with pip, how can I make sure other people can run my program without having that module installed? 如何使Python程序可执行文件,使其自动运行 - How can I make a Python program executable so that it automatically runs 我怎样才能使运行该程序的其他用户不需要手动下载和安装模块? - How can I make it so that manual download and install of modules is not required by another user running the program? 我无法在雇主笔记本电脑上安装适用于python 3.5.2的新模块 - I can't install new modules for python 3.5.2 on my employer laptop 如何让jekins识别我的python安装? - How can I make jekins recognise my python install? 如何使我的程序回到Python的开头? - How can I make my program return to the beginning in Python? 如何让我的 python 程序在后台运行 - How can i make my python program work in background 如何在我的python程序中显示以下文本? - How can I make the following text appear in my python program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM