automatically import module when run python script?

后端 未结 2 457
独厮守ぢ
独厮守ぢ 2021-01-19 15:14

I think other may ask this before but I cannot find it. My question is, I have these statements in my .ipython/ipy_user_conf.py:

ip.ex(\'import          


        
相关标签:
2条回答
  • 2021-01-19 16:00

    You need to modify site.py. This script is run every time the Python program is run. For me, it lives in /usr/lib/python2.7/site.py

    As Chris Morgan says, this is terribly bad practice and I strongly recommend you avoid it.

    0 讨论(0)
  • 2021-01-19 16:02

    It's bad practice to have a script which depends on an external startup script to make it work. Doing these imports is how you should do it.

    You could simplify it if you regularly import a fairly large set of things by centralising your imports in a file (call it common_imports.py, for example), and then importing all from that (from common_imports import *). That then becomes only one line to put in. I would still prefer to see explicit imports, however.

    (Another note: in import os as os, the as os is entirely superfluous.)

    0 讨论(0)
提交回复
热议问题