Run python file with templater

I’m very new in templater and python. So I might be missing something very obvious.

I’m trying to run a python script through templater plugin. My aim is to insert the python result into obsidian. When I try a simple code with numpy it works (like printing out an array of numbers). But when I try to run a file with different libraries such as request or geopy it doesn’t work. The obsidian console prints out “No module named requests” . But I know that they are installed. How can I solve this problem?

Below is the code Im using (by the way the script works and prints out just fine in terminal)

import requests
import json

send_url = "http://api.ipstack.com/check?access_key=MYTOKEN"
geo_req = requests.get(send_url)
geo_json = json.loads(geo_req.text)
latitude = geo_json['latitude']
longitude = geo_json['longitude']

print(latitude,longitude)
1 Like

Maybe it’s using another path?

Can you check with:

import sys
print(sys.path)

and check if the requests lib is there?

Thank you for responding. I also think its a path issue. But Im not really understanding. “print(sys.path)” prints out the following:

‘/Users/nat/opt/anaconda3/lib/python37.zip’, ‘/Users/nat/opt/anaconda3/lib/python3.7’, ‘/Users/nat/opt/anaconda3/lib/python3.7/lib-dynload’, ‘/Users/nat/.local/lib/python3.7/site-packages’, ‘/Users/nat/opt/anaconda3/lib/python3.7/site-packages’, ‘/Users/nat/opt/anaconda3/lib/python3.7/site-packages/pip-21.2.2-py3.7.egg’

Im sorry, Im not sure how to check if request is there. When I try to install requests via pip it says the following (maybe this can help):

already satisfied: requests in ./opt/anaconda3/envs/thesis/lib/python3.7/site-packages (2.23.0)

I think pip installed the ‘requests’ library in the anaconda environment ‘thesis’, but templater somehow is using another (default?) environment.

I ignore how to configure python in templater, perhaps the environment needs to be selected in the config

conda activate thesis

Or perhaps install the library from the code itself, I don’t know :slight_smile:

import subprocess
import sys

subprocess.check_call([sys.executable, "-m", "pip", "install", "requests"])

There are many python interpreters (python bins) in your PC and the one OBsidian uses is not the one has requests installed.

Maybe you could set the correct python interpreters explicitly.

#!/path/to/python

import requests
import json

send_url = "http://api.ipstack.com/check?access_key=MYTOKEN"
geo_req = requests.get(send_url)
geo_json = json.loads(geo_req.text)
latitude = geo_json['latitude']
longitude = geo_json['longitude']

print(latitude,longitude)

/path/to/python points to a python bin which contains thesis in its path.

1 Like

I tried to install the library from the code with subprocess. It prints “returned non-zero exit status 1”. How can I configure the templater for “conda activate thesis”?

I tried adding the followings to the script (just to be sure) and non worked.

#!/Users/nat/opt/anaconda3/envs/thesis/bin/python3.7
#!/Users/nat/opt/anaconda3/envs/thesis/bin/python
#!/Users/nat/opt/anaconda3/envs/thesis/bin
#!/Users/nat/opt/anaconda3/bin

It still says “No module named requests”.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.