# Compile python script for Connectport X2 / X4 ?

**URL:** <https://forums.digi.com/t/compile-python-script-for-connectport-x2-x4/2795>\
**Category:** Python\
**Created:** [July 12, 2011, 4:13pm UTC](https://forums.digi.com/t/compile-python-script-for-connectport-x2-x4/2795 "2011-07-12T16:13:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![justmark](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@justmark](https://forums.digi.com/u/justmark)\
**Post date:** [July 12, 2011, 4:13pm UTC](https://forums.digi.com/t/compile-python-script-for-connectport-x2-x4/2795/1 "2011-07-12T16:13:51Z")

</div>

Is there a way to compile a python script for the X2/X4 that is also using zigbee.py ? Attempting to compile locally obviously fails as zigbee.py isn’t available.

Is there a way to do this ? Can I do it on the connectport directly, and then copy the file back to a pc so that I can upload it to other connectports ?

thanks.

---

<div class="post-metadata">

**Author:** ![DigiGuy42](https://avatars.discourse-cdn.com/v4/letter/d/59ef9b/32.png) [@DigiGuy42](https://forums.digi.com/u/DigiGuy42)\
**Post date:** [July 12, 2011, 4:27pm UTC](https://forums.digi.com/t/compile-python-script-for-connectport-x2-x4/2795/2 "2011-07-12T16:27:58Z")

</div>

Can you tell us what you have tried? The byte-compilation process doesn’t actually perform any execution of the code inside a module, so you should be able to compile anything that’s syntactically valid just fine.

What I’ve typically done is use the standard library ‘compileall’ script to bytecompile things

python2.4 -m compileall .

Note the period at the end, that specifies the current directory, and the use of python2.4 to ensure that I’m using the correct version of Python for the gateway. This will leave new .pyc files in the directory for each python file.

---

<div class="post-metadata">

**Author:** ![mkotasek](https://avatars.discourse-cdn.com/v4/letter/m/90ced4/32.png) [@mkotasek](https://forums.digi.com/u/mkotasek)\
**Post date:** [July 12, 2011, 4:30pm UTC](https://forums.digi.com/t/compile-python-script-for-connectport-x2-x4/2795/3 "2011-07-12T16:30:29Z")

</div>

Hi justmark,

You can use the py\_compile standard module to byte-compile your code without running it (eg, without importing zigbee.py). Just make sure you are performing the compile operations with a compatible interpreter version with the target device.

Most ConnectPort X devices will use 2.4.x (where any 2.4 python interpreter is compatible). To verify the version of the interpreter, both on the gateway and on the PC performing the compile operation, you can start the python interpreter by calling ‘python’ and it reports the version string automatically.

Hope this helps,  
Max

---

<div class="post-metadata">

**Author:** ![justmark](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@justmark](https://forums.digi.com/u/justmark)\
**Post date:** [July 12, 2011, 4:31pm UTC](https://forums.digi.com/t/compile-python-script-for-connectport-x2-x4/2795/4 "2011-07-12T16:31:42Z")

</div>

Thanks for the quick replies.

I had tried the suggestions on the link provided by Shahrj1988 prior to posting, but it had complained that one of the import files (zigbee.py) wasn’t available. I’ll try that again.

I’ll also try out your suggestion, DigiGuy42.

Thanks.

---

<div class="post-metadata">

**Author:** ![justmark](https://avatars.discourse-cdn.com/v4/letter/j/a87d85/32.png) [@justmark](https://forums.digi.com/u/justmark)\
**Post date:** [July 12, 2011, 7:46pm UTC](https://forums.digi.com/t/compile-python-script-for-connectport-x2-x4/2795/5 "2011-07-12T19:46:53Z")

</div>

Max,

Tried this out. I made sure that I had 2.4.3 loaded onto my Mac, and followed your suggestion. It generated the .pyc file, which I then transferred to my Connectport.

Upon running it there, I get the following error:

#\> python coord.pyc

File “coord.pyc”, line 1  
m?  
^  
SyntaxError: invalid syntax

Any idea on what I’ve done wrong ?

thx.

---

<div class="post-metadata">

**Author:** ![DigiGuy42](https://avatars.discourse-cdn.com/v4/letter/d/59ef9b/32.png) [@DigiGuy42](https://forums.digi.com/u/DigiGuy42)\
**Post date:** [July 12, 2011, 7:50pm UTC](https://forums.digi.com/t/compile-python-script-for-connectport-x2-x4/2795/6 "2011-07-12T19:50:24Z")

</div>

It’s okay to byte-compile all but the main script file for the gateway. You’re trying to run the .pyc file directly. That file should be left a standard .py.

If it’s large and complicated, you could as an option make a very simple script that imports and runs your code and still take advantage of the byte-compiled .pyc.

---

<div class="post-metadata">

**Author:** ![gary\_at\_meters.us](https://avatars.discourse-cdn.com/v4/letter/g/43a26b/32.png) [@gary\_at\_meters.us](https://forums.digi.com/u/gary_at_meters.us)\
**Post date:** [August 28, 2012, 3:21pm UTC](https://forums.digi.com/t/compile-python-script-for-connectport-x2-x4/2795/7 "2012-08-28T15:21:51Z")

</div>

Don’t you just wish that someone would write the step-by-step guide?  
Here’s what it means to compile on a Digi X\_  
Here are the benefits of compiling  
Here are the references to Python (and versions) you should read and refer to.  
Here is how you do it  
Here are different ways to do it  
Here are the options you can use.  
Here is what you put in the auto-start entry  
etc.

Yes - I know we use standard Python.  
But when people start using other devices, sometimes there are nuances to handle.

I love Python on this system, but its not well documented for the transition. Sure - its “easy” for experts and people at Digi.

I still like how it works!
