python 启动时自动加载模块
我希望 IPython 或 Python 解释器在启动时自动加载模块。
是否可以?
例如,当我启动 IPython 时:
$ ipython
...
>>> from __future__ import division
>>> from mymodule import *
In [1]:
在教程页面。
I want IPython or the Python interpreter to auto-load a module when I start them.
Is it possible?
For example when I start IPython:
$ ipython
...
>>> from __future__ import division
>>> from mymodule import *
In [1]:
Something like SymPy's live shell found in the tutorial pages.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在您的主目录中有一个
.pythonstartup
并在其中加载模块,并将PYTHONSTARTUP
env 指向该文件。在以交互模式显示第一个提示之前,将执行该文件中的 Python 命令。
我用它来在 python 解释器 shell 中启用命令行补全
Have a
.pythonstartup
in your home directory and load modules there and pointPYTHONSTARTUP
env to that file.Python commands in that file are executed before the first prompt is displayed in interactive mode.
I use it for enabling command line completion in python interpreter shell
除非将
-S
选项传递给python
二进制文件,否则会使用特殊的 site 模块。除此之外,该模块还会查找*.pth
文件。在每一行中,*.pth
文件应包含要包含到sys.path
中的路径或要执行的命令。该模块还导入sitecustomize
和usercustomize
(它可以包含任意代码,如果他们碰巧引发错误,这是让你的同事发疯的好方法)(如果它们存在于某处)在sys.path
中。但问题是,当导入
site
模块时,当前目录不在sys.path
中,也就是说很难配置您的特定脚本。我有时会在脚本的开头添加以下行,以便脚本首先在当前目录中搜索
.pth
文件并将缺少的路径添加到sys.path:
Unless
-S
option is passed to thepython
binary, a special site module is imported by default before the execution is passed to your script, or the interactive interpreter. Among other things the module looks for*.pth
files. On each line the*.pth
files should contain either a path to include intosys.path
, or a command to execute. The module as well importssitecustomize
, andusercustomize
(which can contain arbitrary code, a good way to make your colleagues crazy, if they happen to raise errors) if they exist somewhere insys.path
.The problem is though, that the current directory in not in
sys.path
when thesite
module is imported, that is it is hard to configure your particular script.I sometimes add the following line at the beginning of my scripts, so that the script would start with searchin for
.pth
files in the current directory and adding the missing paths tosys.path
:检查文件
~/.ipython/ipythonrc
- 您可以列出要在启动时加载的所有模块。Check the file
~/.ipython/ipythonrc
- you can list all modules you want to load at the startup.另一种可能的解决方案是使用 参数
-i
来自python
解释器,在执行脚本后启动交互模式。例如,您可以使用:
python -i your_module.py
python -i /path/to/your/module
如果您定义了__main__.py< /code>
python -i -m your.module
Another possible solution is to use the argument
-i
frompython
interpreter that launches the interaction mode after executing your script.You could for instance use:
python -i your_module.py
python -i /path/to/your/module
in case you have defined a__main__.py
python -i -m your.module
要在使用时自动延迟导入所有顶级可导入模块,请在
PYTHONSTARTUP
文件中定义:现在您可以访问模块,而无需手动导入它们:
To automatically lazily import all top-level importable modules when using them, define this in your
PYTHONSTARTUP
file:Now you can access modules without needing to import them manually: