Issue Importing Dynamically Installed Module at Runtime /u/No_Increase_7127 Python Education

Hey, everyone! I’ve been having lots of trouble installing and importing a module at runtime. I have no other option, since my app is running on a cluster and cannot be restarted, and the module I want to import is custom and is stored on the cloud. Installing it beforehand is also not an option.
Basically, this is what I do:

import sys import os import importlib import subprocess # Install subprocess.run(f"cd /my_libs/FOO/ && pip3 install -e .", check=True, shell=True) # Add to system path sys.path.insert(0, os.path.abspath("/my_libs/FOO/foo")) # Import utils = importlib.import_module("foo") 

When I run this test code, the pip instalation goes fine, but I get a “No module named foo” error on the importlib line. However, if I run the code a second time, it works as expected. This leads me to believe that Python only updates the installed modules on startup.

Has anyone encountered this issue before or have suggestions on how I can make this work? I’ve tried several approaches, but nothing seems to fix it.

Thanks in advance for any help!

submitted by /u/No_Increase_7127
[link] [comments]

​r/learnpython Hey, everyone! I’ve been having lots of trouble installing and importing a module at runtime. I have no other option, since my app is running on a cluster and cannot be restarted, and the module I want to import is custom and is stored on the cloud. Installing it beforehand is also not an option. Basically, this is what I do: import sys import os import importlib import subprocess # Install subprocess.run(f”cd /my_libs/FOO/ && pip3 install -e .”, check=True, shell=True) # Add to system path sys.path.insert(0, os.path.abspath(“/my_libs/FOO/foo”)) # Import utils = importlib.import_module(“foo”) When I run this test code, the pip instalation goes fine, but I get a “No module named foo” error on the importlib line. However, if I run the code a second time, it works as expected. This leads me to believe that Python only updates the installed modules on startup. Has anyone encountered this issue before or have suggestions on how I can make this work? I’ve tried several approaches, but nothing seems to fix it. Thanks in advance for any help! submitted by /u/No_Increase_7127 [link] [comments] 

Hey, everyone! I’ve been having lots of trouble installing and importing a module at runtime. I have no other option, since my app is running on a cluster and cannot be restarted, and the module I want to import is custom and is stored on the cloud. Installing it beforehand is also not an option.
Basically, this is what I do:

import sys import os import importlib import subprocess # Install subprocess.run(f"cd /my_libs/FOO/ && pip3 install -e .", check=True, shell=True) # Add to system path sys.path.insert(0, os.path.abspath("/my_libs/FOO/foo")) # Import utils = importlib.import_module("foo") 

When I run this test code, the pip instalation goes fine, but I get a “No module named foo” error on the importlib line. However, if I run the code a second time, it works as expected. This leads me to believe that Python only updates the installed modules on startup.

Has anyone encountered this issue before or have suggestions on how I can make this work? I’ve tried several approaches, but nothing seems to fix it.

Thanks in advance for any help!

submitted by /u/No_Increase_7127
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *