Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Editor

Notices

Reply
 
Thread Tools Search this Thread
Old 12-24-2022, 10:13 AM   #1
vr8ce
Enthusiast
vr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchfork
 
vr8ce's Avatar
 
Posts: 33
Karma: 48958
Join Date: May 2017
Device: iPad Pro 9.7"
Import module for use in regex function?

I have a (pip installable) python module I want to use in a Calibre regex function that does not exist in calibre's python environment. I therefore get an error when trying to save the function.

Is it possible to add external modules to calibre's python environment, not for development, but just for use in a custom regex function? IOW, I don't want to run calibre in development mode, I just want to be able to use a module that calibre doesn't have installed.

I've looked through the regex function portion of the manual, and done a bit of searching here, and have not found anything.
vr8ce is offline   Reply With Quote
Old 12-24-2022, 05:38 PM   #2
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 45,355
Karma: 27182818
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
It's python if you want to import something from somewhere add it to sys.path before doing the import. Assuming your external module is compatible with the version of python it will work fine.
kovidgoyal is offline   Reply With Quote
Advert
Old 12-24-2022, 09:02 PM   #3
vr8ce
Enthusiast
vr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchfork
 
vr8ce's Avatar
 
Posts: 33
Karma: 48958
Join Date: May 2017
Device: iPad Pro 9.7"
Quote:
Originally Posted by kovidgoyal View Post
It's python if you want to import something from somewhere add it to sys.path before doing the import. Assuming your external module is compatible with the version of python it will work fine.
Forgive my ignorance; I use python regularly, but I'm not sure how to deal with calibre's built-in one.

Do you mean my regular python's sys.path, or calibre's sys.path? The module is in the former; if I run python3 at the command line and import the module, it works fine. But doing the same import in a calibre regex function returns the error. Nor do I know what calibre's sys.path is; my local version has all version-dependent directories, so I assume calibre's does as well.
vr8ce is offline   Reply With Quote
Old 12-24-2022, 10:13 PM   #4
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 45,355
Karma: 27182818
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
In the calibre function do

sys.path.append('whatever')
import your_module
do whatever you want with it
sys.path.remove('whatever')
kovidgoyal is offline   Reply With Quote
Old 12-24-2022, 11:39 PM   #5
vr8ce
Enthusiast
vr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchforkvr8ce can load mercury with a pitchfork
 
vr8ce's Avatar
 
Posts: 33
Karma: 48958
Join Date: May 2017
Device: iPad Pro 9.7"
Ahh, so add the directory where I have the module to the system path temporarily. Excellent, thanks very much; still so much in python I've never had to do.
vr8ce is offline   Reply With Quote
Advert
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with S&R RegEx Function MerlinMama Editor 5 05-29-2022 02:23 AM
Predefined regex for Regex-function sherman Editor 3 01-19-2020 05:32 AM
Regex Function about «» and “” senhal Editor 8 04-06-2016 02:12 AM
Import module not in bundled Python Toxaris Plugins 11 02-01-2016 01:55 AM
No Module name Tkinter on plugin import foghat Plugins 1 11-11-2010 07:11 PM


All times are GMT -4. The time now is 12:21 PM.


MobileRead.com is a privately owned, operated and funded community.