Geany custom command not working - python

I have following code as custom python script:
#!/usr/bin/env python
import sys
import re
from sys import stdin
sys.stdout.write(DOC_FILENAME)
How can I use this as custom command in Geany to get current document name and path ?
Is "geanypy" needed to be installed to run this command ?

Related

Can't import Python module even though it is in the PYTHONPATH

I'm trying to run Pixar's USDZ converter tool but i can't seem to run the Python script, it complains with the error Error: failed to import pxr module. Please add path to USD Python bindings to your PYTHONPATH.
I am doing this on Ubuntu instead of the native MacOS, but i have compiled from source the USD library, located in /usr/local/USD/.
Runnig the script outputs the above error:
$ ./usdzconvert -h
Error: failed to import pxr module. Please add path to USD Python bindings to your PYTHONPATH
even though I have added the path to the PYTHONPATH:
$ echo $PYTHONPATH
:/usr/local/USD/lib/python:/usr/local/USD/lib/python/pxr:/usr/local/USD/lib
furthermore Python seems to see that just fine:
$ python3 -c 'import sys; print(sys.path)'
['', '/home/julien', '/usr/local/USD/lib/python', '/usr/local/USD/lib/python/pxr', '/usr/local/USD/lib', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages']
I've also tried adding print(sys.path) to the begining of the script to confirm it was loading the same env variable and running it i get the same path as above.
The script portion that throws the error is as follows:
#!/usr/bin/env python3
import os.path
from os import chdir
import sys
import importlib
import tempfile
from shutil import rmtree
import zipfile
usdLibLoaded = True
kConvertErrorReturnValue = 2
if sys.version_info.major != 3:
print(' \033[93mWarning: It is recommended to use Python 3. Current version is ' + str(sys.version_info.major) + '.\033[0m')
try:
from pxr import *
import usdUtils
except ImportError:
print(' \033[91mError: failed to import pxr module. Please add path to USD Python bindings to your PYTHONPATH\033[0m')
usdLibLoaded = False
I don't know anything about python so I am at loss to figure out this import issue, any ideas?
I have python 2 and 3 installed:
$ python --version
Python 2.7.17
python3 --version
Python 3.6.9
but considering the script starts off with a shebang instructing to use python3 i would think this would be fine, no?
I don't known why, but sometimes, something like this worked for me:
PYTHONPATH=. ./usdzconvert -h
How about this:
#!/usr/bin/env python3
import os.path
from os import chdir
import sys
import importlib
import tempfile
from shutil import rmtree
import zipfile
usdLibLoaded = True
kConvertErrorReturnValue = 2
if sys.version_info.major != 3:
print(' \033[93mWarning: It is recommended to use Python 3. Current version is ' + str(sys.version_info.major) + '.\033[0m')
pxr_path = '/usr/local/USD/lib/python/pxr'
sys.path.append(pxr_path)
sys.path.append(os.path.dirpath(pxr_path))
try:
from pxr import *
import usdUtils
except ImportError:
print(' \033[91mError: failed to import pxr module. Please check the path to USD\033[0m')
usdLibLoaded = False
So I've finally figured out what the problem was.
I've modified the python script to print the actual import message and this is what I got:
dynamic module does not define module export function (PyInit__tf)
A quick google search brought me to this answer that explains that the module was compiled with python 2, so I recompiled USD with python 3 and re-installed all its corresponding v3 dependencies and then I ran the script again and this time no more import error.

Python/Linux/Streamlit: executing subprocess for linux script

I'm a bit turned around on how to execute a shell script file in a Linux environment via Python's subprocess command in Streamlit. Any assistance on what I'm missing is appreciated.
I'm using a shell script called 0_texts.sh to run Pylanguagetool for a grammar check of one text file and return corrections in another text file, like so:
cd /home/user/dir/TXTs/
pylanguagetool text_0.txt > comments_0.txt
This script runs correctly in the Linux terminal, writing a comments_0.txt with appropriate grammar checks from text_0.txt.
I need to create a Python/Streamlit app that runs these shell scripts. In attempting to run this shell script, I've written script.py below:
import os
import subprocess
import sys
subprocess.run(['bash','/home/user/dir/Scripts/0_texts.sh'])
I then run script.py in Streamlit via the code below, keeping with Streamlit's documentation on using subprocess here.
import streamlit as st
import os
import subprocess
import sys
def app():
button1 = st.button("Click me")
if button1:
p = subprocess.run([f"{sys.executable}", "/home/user/dir/pages/script.py"])
st.write(p)
When I execute the script.py via Streamlit, the 0_txts.sh script executes, writing comments_0.txt in the correct directory and providing the following traceback: CompletedProcess(args=['/usr/bin/python3', '/home/user/dir/pages/script.py'], returncode=0). However, the comments_0.txt output contains the error input file is required, as if it can't properly access or read text_0.txt. I've tinkered around trying to find the problem, but have hit a brick wall.
Any suggestions on what I'm missing, or paths forward? Any help greatly appreciated.

Run .sh script from python from specific folder

I am trying to run a .sh script from python.
I saw that this can be done in various ways such as:
import subprocess
subprocess.call(["./test.sh"])
or
import os
os.system("sh test.sh")
However this assumes that test.sh is in the same folder where you are running the script from. What if I want to run the .sh which is in a specific folder?
I tried the following but with no luck:
import subprocess
subprocess.call(["cd ~/ros_ws", "./intera.sh"])
import subprocess
subprocess.call(["cd ~/ros_ws", "./intera.sh"], shell=True)
Thanks for the help.
The subprocess.call has an cwd function argument (change working directory)
import subprocess
subprocess.call(["./intera.sh"], cwd="~/ros_ws")

Can't find a Python module in AppleScript

I created a Python script that uses the Todoist API to export my task list to a list of strings (I want to paste them as Discord messages) and it works fine if I run the command on the Terminal window, but if I run the same command using AppleScript, an error is thrown, saying No module named todoist.api.
Here's the code: (I have only one line)
do shell script "$HOME/tasks.sh"
The shell scripts runs the python scripts I need, but the problem is in the imports of those files, which are:
from datetime import date, datetime, timedelta
from todoist.api import TodoistAPI
import pyperclip
import os
import MyTodoist
in the first file (tasks.py) and
from datetime import date, datetime, timedelta
from todoist.api import TodoistAPI
import pyperclip
import os
in the second file (MyTodoist.py)
The final goal is to store the result of a function in tasks.py in a variable in my script, but I don't know how. Can someone help me please?

import modules in python

when i write import win32print in my python command line (IDLE) it works fine and it is imported...
but when i write import win32print in my py file then it gives an error
says no module named win32print
why am i not able to load the module when writing the same statement in a py script...im using windows XP
Check if you are running on the same installation. I'd do that by looking the path:
import sys
print sys.path
And compare the output on IDLE and when you run the .py

Categories