# Importing python library - the wrong way?

**URL:** <https://forum.inductiveautomation.com/t/importing-python-library-the-wrong-way/40527>\
**Category:** Maker\
**Created:** [November 11, 2020, 1:02pm UTC](https://forum.inductiveautomation.com/t/importing-python-library-the-wrong-way/40527 "2020-11-11T13:02:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kb5won](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kb5won/32/4967_2.png) [@kb5won](https://forum.inductiveautomation.com/u/kb5won)\
**Post date:** [November 11, 2020, 1:02pm UTC](https://forum.inductiveautomation.com/t/importing-python-library-the-wrong-way/40527/1 "2020-11-11T13:02:07Z")

</div>

I thought it would be an easy task to get my weather station data into ignition. I learned that my weather station has an API available, so that is the path I took. It suggests using their premade helper functions ([https://ambientweather.docs.apiary.io/#introduction/authentication](https://ambientweather.docs.apiary.io/#introduction/authentication)) so I thought the best choice would be to use python ([https://github.com/avryhof/ambient\_api](https://github.com/avryhof/ambient_api)).

I noted that the newest release of the helper functions don’t work with Python 2.7, so I downloaded the source files for a previous version that did support 2.7. I copied them into the /usr/local/bin/ignition/user-lib/pylib folder, and then tested it out in ignition’s script console. I did it this way because I figured issuing the install command “pip install ambient\_api” would write to the computer’s local installation, not ignition’s installation. Each time I tried to import the library, I got a dependency error, so I found the library files for that dependency and copied them into the pylib folder. I did this about 6 times and decided this can’t be the right way to go about this.

I’m new to the concept of APIs and python libraries and I’m not sure I’m doing any of this right. Any help in this area would be appreciated.

1. Am I on the right path for getting weather station data into ignition?
2. Is there a better way to import python libraries?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 11, 2020, 1:07pm UTC](https://forum.inductiveautomation.com/t/importing-python-library-the-wrong-way/40527/2 "2020-11-11T13:07:07Z")

</div>

1. No idea
2. Use jython’s pip. See the instructions at these links:

[http://forum.inductiveautomation.com/search?q=jython%20ensurepip](http://forum.inductiveautomation.com/search?q=jython%20ensurepip)

Note that if any dependency uses C extensions to python, you won’t be able to make it work.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 11, 2020, 4:55pm UTC](https://forum.inductiveautomation.com/t/importing-python-library-the-wrong-way/40527/3 "2020-11-11T16:55:15Z")

</div>

You can also skip the SDK approach entirely, and just build your own using `system.net.httpClient()`. You can even wrap that up in your own mini-SDK (it doesn't look like the Ambient Weather REST API is very complex) to make it a bit easier to use. I went into the (very broad strokes) here:

> [@Ignition and (Docker) Portainer API. (HTTP queries)](http://forum.inductiveautomation.com/t/ignition-and-docker-portainer-api-http-queries/36725/2):
>
> Http bindings aren’t going to work well for this purpose - they’re not very good at authentication right now. I’d recommend [system.net.httpClient()](https://docs.inductiveautomation.com/display/DOC80/system.net.httpClient). You could wrap things in a class to make accessing the API a little friendlier: class PortainerClient(object): def \_\_init\_\_(self, baseUrl, username, password, \*\*kwargs): self.baseUrl = baseUrl self.client = system.net.httpClient(\*\*kwargs) response = self.client.post(baseUrl + "auth", data={"Username": username, "Password": password}) self…

---

<div class="post-metadata">

**Author:** ![kb5won](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kb5won/32/4967_2.png) [@kb5won](https://forum.inductiveautomation.com/u/kb5won)\
**Post date:** [November 11, 2020, 5:12pm UTC](https://forum.inductiveautomation.com/t/importing-python-library-the-wrong-way/40527/4 "2020-11-11T17:12:38Z")

</div>

> [@paul-griffith](#):
>
> SDK

I like the idea! I'm not knowledgeable enough to figure this out yet!

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 11, 2020, 6:27pm UTC](https://forum.inductiveautomation.com/t/importing-python-library-the-wrong-way/40527/5 "2020-11-11T18:27:35Z")

</div>

The last commit in the Python registry you linked is titled ‘drop Python 2 support’, so you might not have much luck.  
But, really, it looks like the API is pretty simple to wrap. Try this out:

```python
class AmbientWeatherClient(object):
	def __init__ (self, applicationKey, apiKey):
		self.baseUrl = "https://api.ambientweather.net/v1/"
		self.authorization = {
			"applicationKey": applicationKey,
			"apiKey": apiKey
		}	
		self.client = system.net.httpClient()
		
	def call(self, endpoint, **kwargs):
		kwargs.update(self.authorization)
		response = self.client.get(self.baseUrl + endpoint, params=kwargs)
		return response.json

	def get_devices(self):
		# https://api.ambientweather.net/v1/devices?applicationKey=&apiKey=
		return self.call("devices")

	def get_data(self, macAddress, endDate=None, limit=None):
		# https://api.ambientweather.net/v1/devices/macAddress?apiKey=&applicationKey=&endDate=&limit=288
		kwargs = {"macAddress": macAddress}
		if endDate is not None:
			kwargs["endDate"] = endDate
		if limit is not None:
			kwargs["limit"] = limit
		return self.call("devices", **kwargs)
		
client = AmbientWeatherClient("yourApplicationKey", "yourApiKey")

print client.get_devices()

print client.get_data("yourDeviceMacAddress")

```

---

<div class="post-metadata">

**Author:** ![kb5won](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kb5won/32/4967_2.png) [@kb5won](https://forum.inductiveautomation.com/u/kb5won)\
**Post date:** [November 11, 2020, 7:19pm UTC](https://forum.inductiveautomation.com/t/importing-python-library-the-wrong-way/40527/15 "2020-11-11T19:19:27Z")

</div>

😆  
I appreciate your help! I’ve learned a lot from this exercise. Now I need to learn how to get the JSON string into ignition tags.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [November 11, 2020, 7:21pm UTC](https://forum.inductiveautomation.com/t/importing-python-library-the-wrong-way/40527/16 "2020-11-11T19:21:11Z")

</div>

In theory, you can directly write it to a Document type tag, and then there’s a `jsonGet` expression function you could use in derived tags to extract relevant pieces of info.
