0

I have a python module that I'm installing using setup.py. In one of the submodules, say, module_name.submodule, I'm doing a one-time calculation that takes a lot of time. The numbers that are produced won't change, and I will need them for various other calculations. Consequently, every time I load the python module in a script, it takes a long time. Is there any way to calculate and save these numbers only when the python package is being installed?

setup.py looks like this:

from setuptools import setup, find_packages
import module_name

setup(
      name = 'module_name',
      ...
      packages = find_packages()
     )

module_name/submodule.py looks like this:

def func1():
    ...

CONST_VARS = []
... calculate here (takes a long time)...

What is the best way to handle this situation? I don't want to write the constant vars into the script by hand (or to a file or something).

RandomGuy
  • 3
  • 2
sodiumnitrate
  • 2,899
  • 6
  • 30
  • 49
  • 2
    Not sure, but why not do the calculations in setup.py and save them in some text files and read those files in execution or on import – Ashenguard Jul 28 '23 at 17:58
  • 3
    Does this answer your question? [Post-install script with Python setuptools](https://stackoverflow.com/questions/20288711/post-install-script-with-python-setuptools) – mkrieger1 Jul 28 '23 at 17:59
  • @Ashenguard would the idea be to do the calculations, pickle the results or something, and load it when the module is loaded? – sodiumnitrate Jul 28 '23 at 19:34
  • @mkrieger1 not quite, because I don't know where to keep the produced data. That is, do I pickle it somewhere and load it every time the module is loaded? If so, where do I store it? – sodiumnitrate Jul 28 '23 at 20:21
  • 1
    You probably have to combine it somehow with https://stackoverflow.com/questions/7522250/how-to-include-package-data-with-setuptools-distutils (possibly not use a post-install script after all, but create the data files as part of setup.py as suggested in another comment). – mkrieger1 Jul 28 '23 at 20:26
  • 1
    Have a look at this https://python-packaging.readthedocs.io/en/latest/non-code-files.html and what mkrieger1 has suggested you should be able to mix them and get a result – Ashenguard Jul 29 '23 at 06:47
  • @Ashenguard That should do it -- thanks! – sodiumnitrate Aug 01 '23 at 16:10

0 Answers0