importing external libraries

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

importing external libraries

Post by chinhdo » Fri May 24, 2019 7:38 pm

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 » Tue May 28, 2019 7:37 pm

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: 96
Joined: Sat May 28, 2016 8:52 pm

Re: importing external libraries

Post by cgrafx » Tue May 28, 2019 10:22 pm

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 » Thu May 30, 2019 5:38 pm

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 » Fri May 31, 2019 12:36 am

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: 96
Joined: Sat May 28, 2016 8:52 pm

Re: importing external libraries

Post by cgrafx » Fri May 31, 2019 4:52 am

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 » Fri May 31, 2019 11:20 pm

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