[SOLVED] import module from filesystem
Posted: Wed Aug 08, 2018 1:46 pm
Hello guys,
I'm currently working on a project that involve importing selected modules (https://github.com/yeyeto2788/MicroPythonScripts) that are already in the filesystem.
So let's say I have my script call `menu.py` and it will get all files on the filesystem and if I select one of them I want that module to be executed.
Files in the filesystem:
So this function will give the `test.py` and the `abs.py` and import the `test.py` script and execute the function `show()` on it which is simply prints a string:
I get an error saying the following:
Is there any possible solution for this? or anyone that could help me out with this?
I'm currently working on a project that involve importing selected modules (https://github.com/yeyeto2788/MicroPythonScripts) that are already in the filesystem.
So let's say I have my script call `menu.py` and it will get all files on the filesystem and if I select one of them I want that module to be executed.
Files in the filesystem:
- menu.py
- boot.py
- test.py
- abs.py
Code: Select all
import time, os
def get_files():
extensions = ".py"
excluded_files = ["main.py", "boot.py"]
dirfiles = []
for filename in os.listdir():
if filename.lower().endswith(extensions):
if filename.lower() not in excluded_files:
dirfiles.append(filename.replace(extensions, ""))
return dirfiles
a = get_files()[1]
import a
a.show()
Code: Select all
print("wujuuuu imported!")
I get an error saying the following:
Code: Select all
b'Traceback (most recent call last):\r\n File "<stdin>", line 12, in <module>\r\nImportError: no module named \'a\'\r\n')