2

I am trying to pip install a .whl file dynamically (the name of the file will change each time I run it) and import it from the same python script. I tried running it in a different subprocess but I am not able to access the import from within the same file. I think python's importlib might have the answer but I haven't been able to figure it out.

import subprocess
import sys

@staticmethod
def install_file(lib):
    subprocess.check_call([sys.executable, "-m", "pip3", "install", lib]) 

# pip install the whl
install_file(whl_file_name)

# dynamically get the name of the .whl file and import it
whl = __import__(whl_file_name)

whl.do_something()

returns the following error

Traceback (most recent call last):
  File "<stdin>", line 12, in <module>
ModuleNotFoundError: No module named 'name_of_whl_file'
6
  • 2
    This sounds like an XY problem. Focus on the problem, not what you think the solution is. What are you trying to accomplish by doing this? Commented May 6, 2020 at 20:35
  • are you sure the subprocess.check_call was successful? Commented May 6, 2020 at 22:17
  • There is a library I want to use that is stored in a whl file. It can be used by pip installing the whl file. I need to pip install it from a script and then access that library in the same script but I don't know how to Commented May 6, 2020 at 22:23
  • 1
    "I need to pip install it from a script and then access that library in the same script"—again, why do you think you need to do this? Why can't you install it outside of the script? Again, focus on the problem, not whatever you think the solution is. There's probably a better solution. Please read the XY problem link I posted in my first comment. Commented May 7, 2020 at 12:34
  • Maybe you could take some inspiration from this code: github.com/nvbn/import_from_github_com/blob/… Commented Jul 13, 2020 at 11:50

1 Answer 1

1

I had a similar problem. Solved it this way:

def install_file(package: str):
    subprocess.check_call([sys.executable, "-m", "pip", "install", "--no-cache-dir", "--no-index", "--find-links", "whlFolder", package], shell=True)

This method installs de package from the package name (e.g: 'tensorflow') instead of the filename, searching for the lib file in folder 'whlFolder'.

The package parameter must be informed with the package/ library name and the version (optionally). E.g:

#Installing the latest available version
install_file('tensorflow')

#Installing a specific version
install_file('tensorflow==2.2.0')
Sign up to request clarification or add additional context in comments.

5 Comments

But can you then import the newly installed packages within the same process?
If you ignore the version parameter when calling pip command I guess it works. But keep in mind that your whl file must be updated.
I am quite sure this doesn't work, it is not possible to import the newly installed packages. You might want to read the question again.
I guess you're wrong. First because I said that I had a 'similar problem'. Also, I've tested with tensorflow 2.2.0 and tensorflow 2.3. By ommiting version parameter (e.g. ==2.3) when using pip install, it automatically installs the most recent version. The code I've shown works, and this strategy either. The package parameter above might be informed with or without version.
OK. I am confused... I did a quick test and it indeed works, where I was 100% convinced it could not possibly work. I would change my vote, but I can not unless you edit your answer. -- But to me the question is still not clearly answered, although I must say the question is not clear to begin with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.