Hi, I am having troubles figuring how to import an external library/module. I am new to Python. I've tried a few things:
- import mylib
- import mylib.py
- putting "mylib.py" in System and "import System/mylib"
I keep getting error like this:
Traceback (most recent call last):
File "C:\Program Files (x86)/ENTTEC/DMXIS/Macros//A_Dims/66Percents.py", line 1, in <module>
import Lib
ImportError: No module named Lib
Appreciate any help in advance.
Chinh
importing external libraries
Re: importing external libraries
Anyone? It would be really nice to be able to centralize reusable functions in an external library/file. I won't have to copy/paste frequently used functions everywhere.
Re: importing external libraries
File "C:\Program Files (x86)/ENTTEC/DMXIS/Macros//A_Dims/66Percents.py", line 1, in <module>
I'm not a Python programmer, but if that traceback line is copy/pasted, than there is a path error at the //
you can't have an undefined folder path
I'm not a Python programmer, but if that traceback line is copy/pasted, than there is a path error at the //
you can't have an undefined folder path
Re: importing external libraries
Thanks cgrafx.
Yeah that "//" looks suspicious. I am at work right now so I can't try it with DMXIS but I was able to at least learn how to import properly in a regular python script. To import an external file in a sub-folder (assuming your main script is in the root folder and the import file is name mylib.py and located in a sub-folder named lib):
- In the lib folder, create an empty file named __init__.py
- In the main script do "from lib import mylib"
Full working code:
test.py (main script):
lib/mylib.py
Alternatively, you can just have mylib.py in the same folder and then you can just do "import mylib" without having to create a "__init__.py" file
I'll try this in DMXIS when I get home to see if it works.
Yeah that "//" looks suspicious. I am at work right now so I can't try it with DMXIS but I was able to at least learn how to import properly in a regular python script. To import an external file in a sub-folder (assuming your main script is in the root folder and the import file is name mylib.py and located in a sub-folder named lib):
- In the lib folder, create an empty file named __init__.py
- In the main script do "from lib import mylib"
Full working code:
test.py (main script):
from lib import mylib num = mylib.test() print("Hello world! The number is %d." % num)
lib/mylib.py
def test(): return 5
Alternatively, you can just have mylib.py in the same folder and then you can just do "import mylib" without having to create a "__init__.py" file
I'll try this in DMXIS when I get home to see if it works.
Re: importing external libraries
I got it working in DMXIS by putting mylib.py in a folder named lib with an empty file named "__init__.py". In my main script file I can call functions in mylib.py like this:
Hope this helps another DMXIS'er.
from lib import mylib ... mylib.reset() mylib.disableAll() mylib.selectMovingHeads() mylib.enable() mylib.dim(0.5) SaveCurrentPreset()
Hope this helps another DMXIS'er.
Re: importing external libraries
Thanks for the follow up and good to hear you got it working.
Cheers.
Cheers.
Re: importing external libraries
Thanks cgrafx.
Another update... in your library file you need to import DMXIS libraries in order to call DMXIS functions like GetChName, etc. from there. Also it seems like DMXIS does not automatically reload imported files. If you make changes to the imported libraries, you have to restart DMX for the changes to take effect.
Updated working code:
create empty file named __init__.py in lib folder
lib/mylib.py:
test.py:
Another update... in your library file you need to import DMXIS libraries in order to call DMXIS functions like GetChName, etc. from there. Also it seems like DMXIS does not automatically reload imported files. If you make changes to the imported libraries, you have to restart DMX for the changes to take effect.
Updated working code:
create empty file named __init__.py in lib folder
lib/mylib.py:
import sys import _DmxApi from _DmxApi import * def disableAll(): for ch in range(0,512): SetChEnabled(ch, 0)
test.py:
from lib import mylib mylib.disableAll()