问题:输入python或ipython解释器时自动导入模块
我发现自己import numpy as np
几乎每次启动python解释器时都要输入。如何设置python或ipython解释器,以便自动导入numpy?
I find myself typing import numpy as np
almost every single time I fire up the python interpreter. How do I set up the python or ipython interpreter so that numpy is automatically imported?
回答 0
使用环境变量PYTHONSTARTUP。根据官方文档:
如果这是可读文件的名称,则在以交互方式显示第一个提示之前,将执行该文件中的Python命令。在与执行交互命令相同的命名空间中执行文件,以便在其中定义或导入的对象可以在交互会话中使用而无需限定。
因此,只需使用import语句创建一个python脚本,然后将环境变量指向该脚本即可。话虽如此,请记住,“显式总是比隐式更好”,因此不要在生产脚本中依赖此行为。
对于Ipython,请参阅本教程,了解如何制作ipython_config文件
Use the environment variable PYTHONSTARTUP. From the official documentation:
If this is the name of a readable file, the Python commands in that
file are executed before the first prompt is displayed in interactive
mode. The file is executed in the same namespace where interactive
commands are executed so that objects defined or imported in it can be
used without qualification in the interactive session.
So, just create a python script with the import statement and point the environment variable to it. Having said that, remember that ‘Explicit is always better than implicit’, so don’t rely on this behavior for production scripts.
For Ipython, see this tutorial on how to make a ipython_config file
回答 1
对于ipython,有两种方法可以实现此目的。两者都涉及位于的ipython的配置目录~/.ipython
。
- 创建一个自定义的ipython配置文件。
- 或者您可以将启动文件添加到
~/.ipython/profile_default/startup/
为简单起见,我将使用选项2。您所要做的就是在目录中放置一个.py
或.ipy
文件,~/.ipython/profile_default/startup
它将自动执行。因此,您可以将其放置import numpy as np
在简单文件中,然后在ipython提示符的命名空间中使用np。
选项2实际上将与自定义配置文件一起使用,但是使用自定义配置文件将使您可以根据特定情况更改启动要求和其他配置。但是,如果您始终希望np
有空,请务必将其放在启动目录中。
有关ipython配置的更多信息。该文档有一个更完整的解释。
For ipython, there are two ways to achieve this. Both involve ipython’s configuration directory which is located in ~/.ipython
.
- Create a custom ipython profile.
- Or you can add a startup file to
~/.ipython/profile_default/startup/
For simplicity, I’d use option 2. All you have to do is place a .py
or .ipy
file in the ~/.ipython/profile_default/startup
directory and it will automatically be executed. So you could simple place import numpy as np
in a simple file and you’ll have np in the namespace of your ipython prompt.
Option 2 will actually work with a custom profile, but using a custom profile will allow you to change the startup requirements and other configuration based on a particular case. However, if you’d always like np
to be available to you then by all means put it in the startup directory.
For more information on ipython configuration. The docs have a much more complete explanation.
回答 2
我使用〜/ .startup.py文件,如下所示:
# Ned's .startup.py file
print("(.startup.py)")
import datetime, os, pprint, re, sys, time
print("(imported datetime, os, pprint, re, sys, time)")
pp = pprint.pprint
然后定义PYTHONSTARTUP =〜/ .startup.py,Python将在启动shell时使用它。
打印语句在那里,因此当我启动外壳程序时,会提醒我它已经生效,并且已经导入了什么内容。该pp
快捷方式实在是太好用太…
I use a ~/.startup.py file like this:
# Ned's .startup.py file
print("(.startup.py)")
import datetime, os, pprint, re, sys, time
print("(imported datetime, os, pprint, re, sys, time)")
pp = pprint.pprint
Then define PYTHONSTARTUP=~/.startup.py, and Python will use it when starting a shell.
The print statements are there so when I start the shell, I get a reminder that it’s in effect, and what has been imported already. The pp
shortcut is really handy too…
回答 3
虽然在大多数情况下创建诸如ravenac95 建议之类的自定义启动脚本是最佳的通用答案,但在要使用的情况下它将无法正常工作from __future__ import X
。如果您有时在Python 2.x中工作,但想使用现代除法,则只有一种方法可以做到这一点。创建配置文件后,编辑profile_default
(对于Ubuntu,位于~/.ipython/profile_default
),然后在底部添加以下内容:
c.InteractiveShellApp.exec_lines = [
'from __future__ import division, print_function',
'import numpy as np',
'import matplotlib.pyplot as plt',
]
While creating a custom startup script like ravenac95 suggests is the best general answer for most cases, it won’t work in circumstances where you want to use a from __future__ import X
. If you sometimes work in Python 2.x but want to use modern division, there is only one way to do this. Once you create a profile, edit the profile_default
(For Ubuntu this is located in ~/.ipython/profile_default
) and add something like the following to the bottom:
c.InteractiveShellApp.exec_lines = [
'from __future__ import division, print_function',
'import numpy as np',
'import matplotlib.pyplot as plt',
]
回答 4
在Linux上,作为可接受答案的更简单替代方法:
只需定义一个别名即可,例如放入alias pynp='python -i -c"import numpy as np"'
您的〜/ .bash_aliases文件中。然后pynp
,您可以使用调用python + numpy ,并且仍然可以仅使用python python
。Python脚本的行为保持不变。
As a simpler alternative to the accepted answer, on linux:
just define an alias, e.g. put alias pynp='python -i -c"import numpy as np"'
in your ~/.bash_aliases file. You can then invoke python+numpy with pynp
, and you can still use just python with python
. Python scripts’ behaviour is left untouched.
回答 5
您可以创建普通的python脚本,也可以根据需要创建import_numpy.py
任何脚本
#!/bin/env python3
import numpy as np
然后用-i
标志启动它。
python -i import_numpy.py
这样,您便可以灵活地只为不同的项目选择所需的模块。
You can create a normal python script as import_numpy.py
or anything you like
#!/bin/env python3
import numpy as np
then launch it with -i
flag.
python -i import_numpy.py
Way like this will give you flexibility to choose only modules you want for different projects.
回答 6
正如ravenac95在他的回答中提到的那样,您可以创建自定义配置文件或修改默认配置文件。此答案是import numpy as np
自动需要的Linux命令的快速视图。
如果要使用名为的定制概要文件numpy
,请运行:
ipython profile create numpy
echo 'import numpy as np' >> $(ipython locate profile numpy)/startup/00_imports.py
ipython --profile=numpy
或者,如果您想修改默认配置文件以始终导入numpy:
echo 'import numpy as np' >> $(ipython locate profile default)/startup/00_imports.py
ipython
查看IPython配置教程,以深入了解配置文件。请参阅.ipython/profile_default/startup/README
以了解启动目录的工作方式。
As ravenac95 mentioned in his answer, you can either create a custom profile or modify the default profile. This answer is quick view of Linux commands needed to import numpy as np
automatically.
If you want to use a custom profile called numpy
, run:
ipython profile create numpy
echo 'import numpy as np' >> $(ipython locate profile numpy)/startup/00_imports.py
ipython --profile=numpy
Or if you want to modify the default profile to always import numpy:
echo 'import numpy as np' >> $(ipython locate profile default)/startup/00_imports.py
ipython
Check out the IPython config tutorial to read more in depth about configuring profiles. See .ipython/profile_default/startup/README
to understand how the startup directory works.
回答 7
我的默认ipython
调用是
ipython --pylab --nosep --InteractiveShellApp.pylab_import_all=False
--pylab
已经有ipython
一段时间了。它导入numpy
和(的一部分)matplotlib
。我添加了该--Inter...
选项,因此它不使用*
导入,因为我更喜欢使用explicit np....
。
这可以是快捷方式,别名或脚本。
My default ipython
invocation is
ipython --pylab --nosep --InteractiveShellApp.pylab_import_all=False
--pylab
has been a ipython
option for some time. It imports numpy
and (parts of) matplotlib
. I’ve added the --Inter...
option so it does not use the *
import, since I prefer to use the explicit np....
.
This can be a shortcut, alias or script.