-2

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 ".

Is there a way to do this in the program without having to open the command prompt?

  • In this thread, they talk about how to run terminal commands from Python. https://stackoverflow.com/questions/89228/how-to-execute-a-program-or-call-a-system-command – Havin Leung Jul 20 '21 at 18:25

2 Answers2

1

Yes, you can use the built-in pip module, which allows you to install python packages within your code.

e.g.

import pip
pip.main(["install", "pygame"])

in order to check for your package, you could use a try-except and check for an ImportError

Ari24
  • 370
  • 5
  • 12
0

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

imxitiz
  • 3,920
  • 3
  • 9
  • 33