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.
Related
I have a program that is working fine locally, but I need to use it in CoCalc on the cloud to share with a group. Everything works fine until the end, when I want to create a scatter graph, in standard way. There are a bunch of other specs, but essentially, it's just
plt.scatter(x, y, color = 'blue')
plt.show()
Nothing tricky. But in Cocalc, it tells me "UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure."
Here are the imports from the beginning of the file:
import nltk
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
I've searched for this error and tried the solutions I've found, such as importing tkinter. So, for instance, if I change the headers to
import nltk
import tkinter as tk
import pandas as pd
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import numpy as np
I get a different error: "Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running."
I've seen instructions about pip installations and such, but a) I'm not sure the installations would make everything work for my group, and b) CoCalc generally has most of the standard packages ready to go, so maybe that's not the problem?
Obviously, I'm not experienced with these issues, so help is much appreciated.
If you're using a Jupyter notebook in CoCalc, then the above will just work fine. Here's an example:
https://cocalc.com/wstein/support/matplotlib-scatter-in-jupyter
If you're writing all of your code as part of a Python program (so, e.g., .py files) and running this from a Terminal, CoCalc will also fully support Tk and this working! However, you have to start a new X11 desktop session in CoCalc by clicking +New --> "Linux Graphical X11 Desktop", then run your Python program from the terminal in the upper left. You'll see any windows that pop up in the main window off to the right.
See https://cocalc.com/wstein/support/scatter-matplotlib-x11
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.
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 am writing a basic Python module which contains a function that plots some data from a Pandas DataFrame. The problem is that I expect users to call this function from both the interactive shell and IPython notebooks. Unfortunately, our interactive shell environment does not have any valid DISPLAY options for matplotlib to use, so I want my function (or the full module) to behave slightly different depending on which environment it is loaded into. (E.g., the default is show the plot without saving in IPython, and save the plot without showing in the shell).
This question gives me a partial answer, in that I can explicitly tell users to write matplotlib.use('Agg') before they ever import my module, but surely there is a more automated backend way to handle this?
You could try checking if you are running into the situation (empty $DISPLAY) and default users to the Agg backend, rather than asking them to do so.
def configure_matplotlib():
import os
if 'DISPLAY' not in os.environ:
if matplotlib.get_backend() != 'Agg':
matplotlib.use('Agg')
import matplotlib
configure_matplotlib()
import matplotlib.pyplot as plt
...
Now matplotlib.use has no effect once pyplot has been imported, but if your users have already done so, they probably have a working configuration anyway.
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()