-2

I do my project. I need to install pip packages in python code. But I get error.

If I try this: pip._internal.main(['install', package]) or this pip.main(['install', package]) I get this: NameError: name 'pip' is not defined. I use python 3.10 and pip 22. For do this code I use exec().

Pyjava
  • 1
  • 1
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Oct 30 '22 at 05:32

1 Answers1

1

Could you do something like this?

import subprocess
import sys


def install_package(package):
    """
    Install a package using pip
    """

    subprocess.check_call([sys.executable, "-m", "pip", "install", package])
PCDSandwichMan
  • 1,964
  • 1
  • 12
  • 23