Author Topic: CPython and Python Packages in IronPython?  (Read 7696 times)

0 Members and 2 Guests are viewing this topic.

MrGlasspoole

  • Jr. Member
  • **
  • Posts: 5
  • Karma: 0
    • View Profile
CPython and Python Packages in IronPython?
« on: September 13, 2014, 06:37:59 AM »
Hi,

Python is relatively new to me and i have some questions.
Is it possible to use CPython instead of IronPython or is it to much linked with VC?

The problem is that i want to also use my Python files with other programs.
Then it looks like there are Python packages that don't work with IronPython.

If i can't use CPython the same way and with the same VC functions/features like with IronPython,
how to i register VC IronPhyton in my system (environment variables)?

Then I'm not sure how to ad packages to the VC IronPython.
The normal way is putting them in the "site-packages" folder but that did not work.
I did put it in PY/Lib and that looks like the import works - but the my code gives me:
Code: [Select]
Line: 722 >>
TypeError: create_connection() got an unexpected keyword argument 'source_address'
In CPython my code works...

Also I'm not sure what part of the unziped package is the right one to put in IronPython.
There is the main folder with the setup.py...
There are subfolders "build" and "src"...
Build has a subfolder "lib"...

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: CPython and Python Packages in IronPython?
« Reply #1 on: September 13, 2014, 11:10:01 AM »
Hi Mr.G.

You ask a number of tricky questions in one post, but I'll do my best to answer them.

Python is relatively new to me and i have some questions.
Is it possible to use CPython instead of IronPython or is it to much linked with VC?
You can only use IronPython as a plugin in VC.  The whole point of IronPython is that it can live inside of a .Net application like VoxCommando.

The problem is that i want to also use my Python files with other programs.
If you want to use your python files with "other programs" (I'm not exactly sure what you mean by this), then just use CPython.


If i can't use CPython the same way and with the same VC functions/features like with IronPython,
how to i register VC IronPhyton in my system (environment variables)?
You can't use the same features with CPython because it can't be used in the plugin.  So you can't call actions and events in VC from the python code, or pass variables between VC and python.  But you can call executables along with command line parameters by using the Launch actions in VC, so if you create a program in CPython you can initiate it from VC, and you may also be able to read console information back from it.

I don't know what you mean by "register VC IronPhyton in my system (environment variables)" or why you would need to do this.  You will need to explain more clearly what you mean by this.

Then I'm not sure how to ad packages to the VC IronPython.
The normal way is putting them in the "site-packages" folder but that did not work.
The correct place to put libraries is in YourVCRootFolder\plugins\PY\Lib
I don't recommend you try to use a setup.py though, you just want to put the actual python library files in there manually.

Also I'm not sure what part of the unziped package is the right one to put in IronPython.
There is the main folder with the setup.py...
There are subfolders "build" and "src"...
Build has a subfolder "lib"...

You should look for the folder in the zip that contains mostly .py files and nothing else.  It is probably called lib but it varies.  If you send me a zip I will take a look and make my best guess.  Even for a given library you may find many versions.

I did put it in PY/Lib and that looks like the import works - but the my code gives me:
Code: [Select]
Line: 722 >>
TypeError: create_connection() got an unexpected keyword argument 'source_address'
In CPython my code works...
I can't help much with this yet because.
1 - I don't know what the "it" is that you put in PY/Lib
2 - I don't know if you mean that you put it in VC\PY\Lib (wrong) or VC\Plugins\PY\Lib
3 - I don't know what library you are talking about
4 - I haven't seen your code.

Just because something runs in CPython does not mean that it will run in IronPython.
I am not aware of any situation where code runs in IronPython, but not in the VC IronPython plugin, but anything is possible.
Usually pure python code will work in IronPython and therefore it will work in VC, but there are some exceptions.  Like this one: http://voxcommando.com/forum/index.php?topic=1319.msg11343#msg11343

Edit: To be more clear about that last example:  That is a problem with IronPython, not with VC.  But the point there is that it works in CPython, and even though it is pure python code and should work in IronPython, certain methods, with certain variables, cause an error in IronPython due do some minor difference from CPython.  The problem there is with ElementTree.  ElementTree works in VC, but in some cases like this one it can throw an unexpected error.
« Last Edit: September 13, 2014, 11:17:09 AM by jitterjames »

MrGlasspoole

  • Jr. Member
  • **
  • Posts: 5
  • Karma: 0
    • View Profile
Re: CPython and Python Packages in IronPython?
« Reply #2 on: September 13, 2014, 12:50:02 PM »
If you want to use your python files with "other programs" (I'm not exactly sure what you mean by this), then just use CPython.
Lets say i have i Python script that does something (sure it does something *lol) and i want to run it from VC and also from outside.
I mean you don't want to write the same code twice - one for VC and one for something else.
Hm, example? Ok, maybe i have a Python script that switches an some lamp (arduino+relay) and i want to run it from VC but also from a website.

So you can't call actions and events in VC from the python code, or pass variables between VC and python.  But you can call executables along with command line parameters by using the Launch actions in VC, so if you create a program in CPython you can initiate it from VC, and you may also be able to read console information back from it.
Whats the difference between i "can't call actions and events" but i can "call executables with command line parameters and read console information back from it"?
For example: You can get information from ZoomPlayer like "filename of playing file" or "runtime".
I know (with my yet little Python knowledge) how to do it in CPython.
If i write that in CPython and know want to get this information from VC if i say: tell me filename.

I don't know what you mean by "register VC IronPhyton in my system (environment variables)" or why you would need to do this.
Normally you register Python in Windows under the "environment variables" that you can run Python stuff without the need to always write the whole path or calling Python first:
https://docs.python.org/2/using/windows.html#finding-the-python-executable
Step 2: http://www.aaronstannard.com/post/2012/08/17/How-to-Setup-a-Proper-Python-Environment-on-Windows.aspx

The error:
I try to get MQTT running with IronPython: https://pypi.python.org/pypi/paho-mqtt
I do everything with MQTT - switching/dimming lights, control ZoomPlayer, read temps, open/close doors...
Thats what MQTT is made for and is the coolest thing of the Internet of things :)

Sure i can call my CPython script from VC with Launch.Hidden py\test.py
Code: [Select]
import sys
import paho.mqtt.publish as publish

publish.single("home/Johnny-Five/ZoomPlayer", "5100 fnPlay", client_id="Gem", hostname="localhost")
This is play/pause and works with ZoomPlayer running on Johnny-Five (my HTPC).

If i run this in the IronPython Plugin tester i get that error - so Paho-MQTT does not work in IronPython.

But lets say i do this in CPython:
Code: [Select]
import sys
import paho.mqtt.publish as publish

def ZoomPlayer_play():
   publish.single("home/Johnny-Five/ZoomPlayer", "5100 fnPlay", client_id="Gem", hostname="localhost")
How do i call/run "ZoomPlayer_play" from VC?

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: CPython and Python Packages in IronPython?
« Reply #3 on: September 13, 2014, 01:39:51 PM »
I mean you don't want to write the same code twice - one for VC and one for something else.
Hm, example? Ok, maybe i have a Python script that switches an some lamp (arduino+relay) and i want to run it from VC but also from a website.
Sometimes you have no choice.  You must adapt your code to run in different environments.  If the code can be run standalone, and requires no input, or can bet set to get its input from command line parameters, then just use whatever language you are comfortable with and call it using the VC's Launch action when you want to initiate it from VC.

If you want to run your code in another program and in VC then you will need to accept that you may need to make changes to your code.  Certainly if you code is going to access VC's variable and methods then you will not be able to use your code in other programs without modifying it.

Whats the difference between i "can't call actions and events" but i can "call executables with command line parameters and read console information back from it"?
It is the difference between running a separate program executable by calling a filename, and accessing an object in memory, in order to use its properties and methods.  It is a huge difference but if you don't know the difference, I am not really inclined to spend the time to teach it to you.

Normally you register Python in Windows under the "environment variables" that you can run Python stuff without the need to always write the whole path or calling Python first:
https://docs.python.org/2/using/windows.html#finding-the-python-executable
Step 2: http://www.aaronstannard.com/post/2012/08/17/How-to-Setup-a-Proper-Python-Environment-on-Windows.aspx
This does not apply to VC.  In VC you load and execute python scripts using actions, not using the command line.  This would only apply if you are using an external python program like CPython.  If you want to launch CPython and pass it a python file to execute you can do that using VC's Launch actions.

The error:
I try to get MQTT running with IronPython: https://pypi.python.org/pypi/paho-mqtt
Maybe it is not compatible with IronPython.  I don't know.  You should do some searches to see if anyone is using it with IronPython, or if there is something special you need to do to make it work.

Sure i can call my CPython script from VC with Launch.Hidden py\test.py
...
This is play/pause and works with ZoomPlayer running on Johnny-Five (my HTPC).
Great.  Why don't you just do that then?



But lets say i do this in CPython:
Code: [Select]
import sys
import paho.mqtt.publish as publish

def ZoomPlayer_play():
   publish.single("home/Johnny-Five/ZoomPlayer", "5100 fnPlay", client_id="Gem", hostname="localhost")
How do i call/run "ZoomPlayer_play" from VC?

It looks like you are just accessing your ZoomPlayer through HTTP get requests.  You can do this using the Scrape action in VC.

Actually I guess it is using TCP.
« Last Edit: September 13, 2014, 01:59:58 PM by jitterjames »

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: CPython and Python Packages in IronPython?
« Reply #4 on: September 13, 2014, 02:06:08 PM »
OK.  I got this to compile as is.

I used the attached file.

If you unzip this file you will see a folder named "src"

under that is a folder named "paho".

All you need to do is copy the "paho" folder into VC\Plugins\PY\Lib\

so you will end up with a folder
Code: [Select]
VC\Plugins\PY\Lib\pahoand then
Code: [Select]
VC\Plugins\PY\Lib\paho\mqtt
etc.

You code compiles but I don't have zoomplayer so I don't know if it actually works.

MrGlasspoole

  • Jr. Member
  • **
  • Posts: 5
  • Karma: 0
    • View Profile
Re: CPython and Python Packages in IronPython?
« Reply #5 on: September 13, 2014, 03:04:48 PM »
Maybe it is not compatible with IronPython.  I don't know.  You should do some searches
Thats what i always do before asking - I search for hours ;)

Great.  Why don't you just do that then?
Cause i don't know how to call a function inside my CPython file if i use the VC Launch action.
I think normally i have this CPython script running all the time (it must cause the MQTT-Client needs to listen for incoming messages) . Then from outside i call a function that i have defined inside that script.

It looks like you are just accessing your ZoomPlayer through HTTP get requests.  You can do this using the Scrape action in VC.
Actually I guess it is using TCP.
Yes it's TCP (cause i can't figure out Win32 API).
From Server to HTPC i send the command (5100 fnPlay) with MQTT.
On the HTPC is just a small Python script (MQTT-Client) listening and then opens a socket to ZoomPlayer
and passing this command to it.
But now i know there is a "Scrape action" and i will check it out :)
Maybe i can read WebSocket streams with it...

You code compiles but I don't have zoomplayer so I don't know if it actually works.
Hm, if i execute:
Code: [Select]
import sys
import paho.mqtt.publish as publish

publish.single("home/Johnny-Five/ZoomPlayer", "5100 fnPlay", client_id="Gem", hostname="localhost")
I always get:
Quote
Line: 722 >>
TypeError: create_connection() got an unexpected keyword argument 'source_address'
in the Log

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: CPython and Python Packages in IronPython?
« Reply #6 on: September 13, 2014, 03:24:19 PM »
That error is occurring on line 722 of the client.py library.

You can view this code to try to figure it out.

Are you sure there is not some other code that you need to call first before using publish?