I would like to automatically import some Python modules that I regularly use into my scripts. E.g. instead of having to type every time
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
have them automatically imported in every script.
Any tips on how to do this will be appreciated.
Thanks,
Anna
Use the PYTHONSTARTUP environment variable to point to a script that has your imports written.
Related
PyCharm has a neat option that allows when pressing ctrl+enter to automatically import libraries (e.g. os, sys, etc).
It used to work for NumPy as well, that is when writing:
z = np.zeros(5)
it would give a suggestions to import numpy as np.
However, after installing several packages this disappeared. Are these suggestions are defined anywhere? Can I change/add to them?
I have this code from scikit docs:
import numpy as np
np.random.seed(1)
I understand what this code does semantically, but I cannot understand what this code actually calls.
Ok, numpy is a name of python module and np is just an alias for that. But what is np.random? Is it module inside of another module?
I found source code on GitHub and random is just a folder inside of numpy directory. So numpy should be a package, not a module?
Numpy is a package, that contains module random which contains method seed.
EDIT: This is not solved in the suggested duplicate; reloading the module
after editing the file doesn't help.
I have a python file "/home/Misc/misc_def.py" collecting some functions that I'm using in several ipython notebooks. The first cell in each notebook is
import csv
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
%matplotlib inline
sns.set_style('white')
from sys import path
path.append('/home/Misc')
import misc_def
However, the strange thing is that sometimes this works (the notebook can find the functions in the file) and sometimes it doesn't. I'm using notebooks in different folders, but I think this shouldn't matter since it's all absolute paths. The errors I get are standard for not finding functions; e.g.
NameError: name 'get_overlap_data' is not defined
Is there something unstable about the way I do it above?
I have a mac OS X Yosimite and I'm using python 2.7.10 and Pycharm as my IDLE. I have pylab installed properly but I cannot use any of its modules.
When a try:
from pylab import show
(or any module) it says
ImportError: cannot import name show
But when I run just the line import pylab I get no errors!
I tried leaving that way and calling the module anyway.
pylab.imshow(...)
But I got the same error obviously. Do I have to install those modules separately?
PS: I'm almost sure the problem has nothing to do with the interpreter
Try importing from matplotlib.pyplot, rather than from pylab (this is now the recommended way to import matplotlib):
From example:
from matplotlib.pyplot import imshow
imshow()
Or:
import matplotlib.pyplot as plt
plt.imshow()
Is it possible to start the bpython interpreter so that it always runs some custom commands when it launches?
In my case I simply want to do:
import numpy as np
import matplotlib.pyplot as plt
I can't see anything in the docs. Anyone know a way?
It is written in the docs, just not clearly labelled as such at: http://docs.bpython-interpreter.org/django.html
Gist of it is you can have an environment variable called PYTHONSTARTUP. bpython will execute this file before you get dropped in the interpreter.
While ikanobori's answer is the way to go here, I thought I show another simple alternative.
import numpy as np
import matplotlib.pyplot as plt
import bpython
bpython.embed(locals_=locals())
This will start up the bpython REPL and load in the local variables and other symbols. This would be useful if you wanted to have more than one customized shell.