Skip to content Skip to sidebar Skip to footer

`importlib` Not Utilising/recognising Path

I am trying to import modules while running my main python script, using a smaller setup.py script. However the importlib command: importlib.util.spec_from_file_location(name, loca

Solution 1:

The location argument passed to spec_from_file_location has to be the full path to the python script:

import importlib.util
spec = importlib.util.spec_from_file_location(
    name='something__else',  # name is not related to the file, it's the module name!
    location='/tmp/solebay/My Project Name/setup.py'  # full path to the script
)
my_mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(my_mod)
print(my_mod)

Out:

success!
<module 'something__else' from '/tmp/solebay/My Project Name/setup.py'>

Post a Comment for "`importlib` Not Utilising/recognising Path"