Timeline for answer to How can I import a module dynamically given the full path? by Sebastian Rittau
Current License: CC BY-SA 4.0
Post Revisions
56 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| May 22, 2025 at 9:07 | comment | added | julaine | interesting that this solution gets more complicated with every python-release | |
| Oct 4, 2023 at 15:35 | history | edited | Nam G VU | CC BY-SA 4.0 |
brief on what the code is about
|
| Apr 14, 2023 at 15:01 | comment | added | zbyszek |
To import a file which does not end .py: import importlib NAME, PATH = 'coverage', '/usr/bin/coverage' loader = importlib.machinery.SourceFileLoader(NAME, PATH) spec = importlib.util.spec_from_file_location(NAME, loader=loader) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) Annoying as hell.
|
|
| May 17, 2022 at 14:43 | history | edited | javidcf | CC BY-SA 4.0 |
Added line to include the loaded module in sys.modules, as suggested by the docs, which ensures other modules can import the module name normally.
|
| Mar 22, 2022 at 11:19 | history | edited | Raven | CC BY-SA 4.0 |
Added link to official docs for Python 3.5+ version
|
| Mar 7, 2022 at 5:24 | comment | added | kevinarpe |
If you need to import class MyClass into the current namespace (like I did during a dynamic Py2 vs Py3 import), try this: MyClass = getattr(foo, "MyClass") Discovered here: stackoverflow.com/a/41678146/257299
|
|
| Feb 18, 2022 at 14:53 | comment | added | djvg | The example in the docs is also quite clear: importing-a-source-file-directly | |
| Jul 13, 2021 at 11:05 | comment | added | ジョージ |
Since no one had mentioned pydoc yet -- here are my two cents: pydoc::importfile()
|
|
| Jun 28, 2021 at 21:19 | comment | added | YoussefDir |
I just wanted a quick solution. So I changed the directory cd "folder" and then I was fine. Hope this helps someone.
|
|
| Mar 24, 2021 at 0:33 | comment | added | Chris A | Isn't module.name always going to be the same as the name of the .py file? | |
| Aug 27, 2020 at 3:42 | comment | added | Myridium |
What is module.name?? I'm importing a file, or something from a file. It does not have a namespace.
|
|
| Jul 3, 2020 at 11:22 | comment | added | Christoph90 | Are they out of their minds establishing 3 different ways for the same thing in these different language versions??? | |
| Mar 3, 2020 at 12:40 | comment | added | drootang |
Using the v3.5+ method described leads to pickling errors. An answer linked above by @mforbes here adds an additional step that appears to fix this: sys.modules[spec.name] = foo
|
|
| Jan 20, 2020 at 10:03 | comment | added | Gulzar |
module.name is the module from which I run?
|
|
| Dec 5, 2019 at 16:18 | comment | added | Marco Castanho |
If the imported file contains any relative import these solutions fail ImportError: attempted relative import with no known parent package
|
|
| Nov 10, 2019 at 15:54 | comment | added | Shai Alon | Python 2.7: TypeError: load_module() takes exactly 4 arguments | |
| Oct 3, 2019 at 16:33 | comment | added | Marcin Wojnarski | Interesting how the code gets LONGER and longer with every new version of Python. Not sure if this is really the "pythonic way" of developing a programming language :/ (same for other things, like print() etc.) | |
| Sep 7, 2019 at 19:42 | comment | added | Andry |
Beware of periods in a file name, because the latest version of the python importlib module still could not handle module file names with periods: bugs.python.org/issue38000
|
|
| Jun 19, 2019 at 10:24 | comment | added | Sebastian Rittau | @Lena It should and in my short test it did. | |
| Jun 18, 2019 at 9:25 | comment | added | Lena |
Should spec.loader.exec_module(foo) also ensure that the script is completely run? I have a set-up where I import a list of a variables (and purposely not a class), that all need to be valid and active within the script from which I'm calling it. In contrast to the regular import statement, however, my variables cannot be further recalled.
|
|
| May 9, 2019 at 12:06 | history | edited | Sebastian Rittau | CC BY-SA 4.0 |
grammar, punctuation
|
| Jul 16, 2018 at 1:10 | comment | added | mforbes | As mentioned by @SamGrondahl, these fail if the module has relative imports. His answer provides a solution for python 3.5+: Is there any similar solution for python 2.7? | |
| May 17, 2018 at 15:24 | comment | added | Sam Grondahl | This almost worked for me but when I'm importing a module structured as a directory with an _init_.py I needed an additional line. See my answer below -- hope it helps somebody! | |
| May 9, 2018 at 2:35 | comment | added | Monty _s Flying Circus | @SebastianRittau Then what is currently the best way for python 3.4? | |
| Feb 1, 2018 at 19:26 | comment | added | setholopolus | @SebastianRittau You're right I guess, because even if you imported a specific module by name they could have replaced it by their own module with the same name. | |
| Feb 1, 2018 at 15:31 | comment | added | Sebastian Rittau | @setholopolus None of those is safe, if you use untrusted user input. | |
| Jan 30, 2018 at 18:56 | comment | added | setholopolus |
Is that top one safe? It seems a bit too close to just "exec"ing whatever the user tells you too. Wouldn't it be safer to add a directory to sys.path, and then ask for the specific module that you want?
|
|
| Dec 15, 2017 at 18:57 | comment | added | JacksonHaenchen | It's mentioned that for Python 2, importlib should be used instead of lib, yet I see no example of using importlib to import a module at a path. Anyone have such an example? | |
| Dec 11, 2017 at 13:28 | comment | added | Sebastian Rittau |
@ColeRobertson That line is just an example to show that you need to prefix any access of the module with foo. (or however you call that variable).
|
|
| Nov 23, 2017 at 13:26 | comment | added | Cole Robertson |
I'm running python 3.6.3 on Sierra 10.12.6 and using option 1 (for python 3.5+). The code works, but when I run the line foo.MyClass() I get the error AttributeError: module 'myFileName' has no attribute 'MyClass', where myFileName is the name of the python file I pass to the first arg of importlib.util.spec_from_file_location("module.name", "/path/to/file.py"). Yet when I comment out the line foo.MyClass() the script executes the imported script without issue. Would someone please explain what foo.MyClass() is doing in the suggested code?
|
|
| Jun 7, 2017 at 2:22 | comment | added | AlexLordThorsen |
importlib.util.spec_from_file_location won't import files that don't end in .py =(
|
|
| Jan 25, 2017 at 21:28 | comment | added | Pedro Cattori | ^I ended up asking this in a separate StackOverflow Question: stackoverflow.com/q/41861427/1490091 | |
| Jan 24, 2017 at 18:22 | comment | added | Pedro Cattori |
For Python 3.5+, if /path/to/file.py imports a sibling implicitly (e.g. import bar to import /path/to/bar.py), the solution yields ModuleNotFoundError: No module named 'bar'. Any way to fix this?
|
|
| Sep 28, 2016 at 12:50 | comment | added | Sebastian Rittau | @Mahesha999 Because importlib.import_module() does not allow you to import modules by filename, which is what the original question was about. | |
| Sep 26, 2016 at 13:13 | comment | added | Mahesha999 |
may I know why importlib.import_module is not mentioned here?
|
|
| Sep 9, 2016 at 9:06 | comment | added | ZigZag | @Paolo Celati In Python 3.5+ You should use importlib.import_module(module_name). Some like this. sys,path.append(path_to_file) module = importlib.import_module(module_name) | |
| Jul 9, 2016 at 6:01 | comment | added | lotsofstorage | I'm in Python 3.5.2 and I've found it only works if the extension of the file is .py | |
| Jun 3, 2016 at 10:05 | comment | added | sorin | If you want a version of the code that works in all versions of Python check stackoverflow.com/a/37611448/99834 | |
| May 22, 2016 at 17:04 | comment | added | rocky | @AXO and more to the point one wonders why something as simple and basic as this has to be so complicated. It isn't in many many other languages. | |
| May 20, 2016 at 6:56 | comment | added | ncoghlan |
Despite the use of "import" in the question, you probably want to use runpy.run_path for execution of Python code as a configuration format, rather than dynamically manipulating the import system.
|
|
| Jan 18, 2016 at 13:11 | comment | added | Sebastian Rittau | SourceFileLoader is not deprecated, but SourceFileLoader.load_module() is. | |
| Jan 9, 2016 at 20:21 | comment | added | Ryne Everett |
I'm not seeing that SourceFileLoader is deprecated in the 3.4+ docs.
|
|
| Dec 7, 2015 at 14:31 | history | edited | Sebastian Rittau | CC BY-SA 3.0 |
deleted 4 characters in body
|
| Dec 7, 2015 at 14:26 | history | edited | Sebastian Rittau | CC BY-SA 3.0 |
Python 3.5+, put later Python versions at the top
|
| Dec 5, 2015 at 18:39 | comment | added | d9k | See new (for december 2015) message in bug discussion (bugs.python.org/issue21436#msg255901): there a third new three-lines-long way to load module in python 3.5! | |
| Jun 30, 2015 at 13:58 | history | edited | Sebastian Rittau | CC BY-SA 3.0 |
tighten Python 3.3 code
|
| Feb 27, 2015 at 1:53 | comment | added | capybaralet | Is the idea that this is replicating: ""import module.name as foo"", if your working directory was /path/to ?? Else, what is foo here?? | |
| May 7, 2014 at 14:55 | history | edited | Sebastian Rittau | CC BY-SA 3.0 |
the argument to load_module is optional
|
| May 5, 2014 at 12:00 | history | edited | Sebastian Rittau | CC BY-SA 3.0 |
add Python 3.4 deprecation
|
| May 3, 2014 at 22:34 | review | Suggested edits | |||
| May 3, 2014 at 22:41 | |||||
| Dec 13, 2013 at 9:33 | history | edited | Sebastian Rittau | CC BY-SA 3.0 |
Python 3.3+
|
| Apr 21, 2013 at 16:32 | comment | added | Brandon Rhodes |
@DanD. — the first argument of imp.load_source() determines the key of the new entry created in the sys.modules dictionary, so the first argument does indeed affect loading.
|
|
| Dec 14, 2011 at 4:51 | comment | added | Dan D. |
@SridharRatnakumar the value of the first argument of imp.load_source only sets the .__name__ of the returned module. it doesn't effect loading.
|
|
| Aug 10, 2009 at 21:54 | comment | added | Sridhar Ratnakumar |
If I knew the namespace - 'module.name' - I would already use __import__.
|
|
| Oct 23, 2008 at 10:22 | vote | accept | derfred | ||
| Sep 15, 2008 at 22:41 | history | answered | Sebastian Rittau | CC BY-SA 2.5 |