importing external libraries

Discuss macro programming, and post any new macros here!
chinhdo
Posts: 5
Joined: Fri May 24, 2019 7:34 pm

importing external libraries

Post by chinhdo »

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
chinhdo
Posts: 5
Joined: Fri May 24, 2019 7:34 pm

Re: importing external libraries

Post by chinhdo »

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.
cgrafx
Posts: 176
Joined: Sat May 28, 2016 8:52 pm

Re: importing external libraries

Post by cgrafx »

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
chinhdo
Posts: 5
Joined: Fri May 24, 2019 7:34 pm

Re: importing external libraries

Post by chinhdo »

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):
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.
chinhdo
Posts: 5
Joined: Fri May 24, 2019 7:34 pm

Re: importing external libraries

Post by chinhdo »

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:

from lib import mylib
...
mylib.reset()
mylib.disableAll()
mylib.selectMovingHeads()
mylib.enable()
mylib.dim(0.5)
SaveCurrentPreset()

Hope this helps another DMXIS'er.
cgrafx
Posts: 176
Joined: Sat May 28, 2016 8:52 pm

Re: importing external libraries

Post by cgrafx »

Thanks for the follow up and good to hear you got it working.

Cheers. ;)
chinhdo
Posts: 5
Joined: Fri May 24, 2019 7:34 pm

Re: importing external libraries

Post by chinhdo »

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:
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()
Post Reply