Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python startup script [duplicate]

Tags:

python

startup

I would like to execute a script work.py in Python, after executing some initialization script init.py.

If I were looking for an interactive session, executing python -i init.py or setting PYTHONSTARTUP=/path/to/init.py would do the trick, but I am looking to execute another script.

Since this is a generic case which occurs often (init.py sets environment and so is the same all of the time), I would highly prefer not referencing init.py from work.py. How could this be done? Would anything change if I needed this from a script instead of from the prompt?

Thank you very much.

like image 408
gt6989b Avatar asked Jul 09 '12 22:07

gt6989b


2 Answers

More generally than in the accepted answer of C0deH4cker, this is discussed in the Python manual in Section 2.2.5 - Cusomization Modules. The basic idea is, to get the location of the special start-up script, one needs to execute the following Python code, e.g. from the interactive session of the interpreter:

>>> import site
>>> site.getusersitepackages()
'/home/user/.local/lib/python3.2/site-packages'

The output should be exactly such a location, in the file sitecustomize.py.

like image 63
gt6989b Avatar answered Nov 02 '22 23:11

gt6989b


Python has a special script that is run on startup. On my platform it is located at /usr/lib/python2.5/site-packages/sitecustomize.py IIRC. So, you could either put init.py in that directory alongside a sitecustomize.py script that imports it, or just paste the content of init.py in the sitecustomize.py.

like image 7
C0deH4cker Avatar answered Nov 02 '22 22:11

C0deH4cker