Spyder default module import list - python

I'm trying to set up a slightly customised version of Spyder. When Spyder starts, it automatically imports a long list of modules, including things from matplotlib, numpy, scipy etc. Is there a way to add my own modules to that list?
In case it makes a difference, I'm using the Spyder configuration provided by the Python(X,Y) Windows installer.

First you have to create a Python file with the modules you want to import at startup. Suppose you call it my_imports.py and that it has this contents:
import numpy as np
import matplotlib.pyplot as plt
Then you have to go to
Tools > Preferences > IPython Console > Startup > Run a file
select the option
Use the following file
and finally click on the button to the right of text field below that option to select your my_imports.py file.

The startup script for Spyder is in site-packages/spyderlib/scientific_startup.py.
Carlos' answer would also work, but this is what I was looking for.

If Spyder is executed as a python script by python binary, then you should be able to simply edit Spyder python sources and include the modules you need. You should take a look into how is it actually executed upon start.

Related

Pandas Installed For All Python Versions But Module Can't Be Found

I am trying to modify an AI for a game on the steam store. The AI communicates through the game with the use of a mod called the communication mod. The AI is made using a python project. The package I am trying to modify is https://github.com/ForgottenArbiter/spirecomm and the mod is https://github.com/ForgottenArbiter/CommunicationMod.
I want to add the pandas package and the job lib package as imports so I can use a model I have made for the AI. When I try to run the game + mod after adding the pandas and joblib packages as imports I get this error in the error log.
Traceback (most recent call last):
File "/Users/ross/downloads/spirecomm-master/main.py", line 6, in <module>
from spirecomm.ai.agent import SimpleAgent
File "/Users/ross/Downloads/spirecomm-master/spirecomm/ai/agent.py", line 10, in <module>
import pandas as pd
ModuleNotFoundError: No module named 'pandas'
This issue only happens when the game is running and the mod tries to run. if I just run the file in terminal it is able to compile/run and send the ready signal
I have checked that I have these modules installed and it is installed. I am on an M1 Mac and I have several different versions of python installed but I have checked them all and it is installed for each of them. I have also opened the python package using pycharm and added pandas and joblib to the python interpreter as a package.
Another thing I have tried is modifying the setup.py file to say that pandas and joblib are required. I then ran it again but I am not sure if this had any effect because I have already run it before.
There is limited help that can be provided without knowing the framework that you are is using but hopefully this will give you some starting points to help.
If you are getting a "No module named 'pandas'" error, it is because you have imported pandas in your code but your python interpreter cannot find it. There are two major reasons this will happen, either it is not installed (which you say has definitely happened) or it is not in the location the interpreter expects (most likely).
The first thing you can do is make sure the pandas install is in the PYTHONPATH. To do this look at Permanently add a directory to PYTHONPATH?.
Secondly, you say you have several versions of python and have installed the module for all versions but you most likely have several instances of at least one version. Many IDEs, such as PyCharm, create a virtual environment when you create a new project and place in it a new instance of python interpreter, or more accurately a link to one version of python. Within this virtual environment, the IDE then loads the modules it has been told to load and thus makes them available for import to the code using that particular environment.
In your case I suspect you may be using a virtual environment that has not had pandas loaded into it. You need to investigate your IDEs documentation if you do not know how to load it. Alternatively you can instruct the IDE to use a different virtual environment (one that does have pandas loaded) - again search documentation for how to do this.
Finally, if all else fails, you can specifically tell your code where to look for the module using the sys.path.append command in your code.
import sys
sys.path.append('/your/pandas/module/path')
import pandas

Matplotlib image not coming up in Visual Studio Code on Mac

I'm running some basic code in the Visual Studio Code editor on MacOSX:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 20, 100)
plt.plot(x, np.sin(x))
plt.show()
...and can't seem to get the png/svg file image to come up after running this. This also doesn't stop executing and I have to manually terminate the process. However, if I run this directly in the Terminal (each line of code line for line) I get the resulting image. One work-around is to just save the file (plt.savefig('foo.png')). This seems to work - the image is saved in the specified file location. However, it would be good to just see the image come up after running the code.
When running matplotlib codes from the terminal, I experience the same kind of hanging of the application after saving the image to a file. In this case, one 'workaround' that has always worked for me is to turn off blocking. Basically alter your code in this way:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 20, 100)
plt.plot(x, np.sin(x))
plt.show(block=False)
input('press <ENTER> to continue')
It's not perfect, but the image is saved correctly and the application stops after you hit ENTER in the terminal. Hope this helps.
I am having a similar issue and I think it is a problem with the exact version of python that vs code is using to run the code.
For reference, I have vscode version 1.52.1 on a mac os Catalina. I installed python via anaconda and created a new environment for python 2.7 (tried with python 3.8 too). I open VSCode by calling code . from the folder I have my simple python codes saved in.
Like OP, I could reproduce the figure if I were to run the code from a python instance called from terminal but not from vscode.
MY SOLUTION:
From vscode, select as python interpreter the one found in /usr/bin/python not the one in ~/opt/anaconda3/env/python27/bin/python
But this is weird, because from a separate terminal window which python returns ~/opt/anaconda3/env/python27/bin/python. This suggests me (though I am no python expert) that there is an issue within vscode about linking the right interpreter with the libraries. Frankly being new to vscode, I find the need to pay attention to these details quite concerning.
I got the same problem, which was driving me nuts. The image was displayed when using Jupyter Notebooks, but not always when using VS Code. I just added one last line_ plt.show() - IMHO unnecessarily - but this worked well.
import matplotlib.pyplot as plt
import matplotlib.image as img
im = img.imread('myimage.png')
plt.imshow(im)
plt.show() # <- added this line and now the image shows every time!
I faced the same issue and here's what I did to solve it.
Since the code runs without any error but also does not generate any plot and needs to be terminated manually, it's difficult to figure out what's going on. I tried running python2.7 test.py
This works, plot is generated but python3 test.py does not work.
So, here's what you need to do -
Run, pip install matplotlib --upgrade to upgrade the matplotlib. This does not resolve the issue but now the error is printed.
"RuntimeError: Python is not installed as a framework" ......
So, finally, to solve the problem, refer to Working with Matplotlib on macOS
Since, I am using Anaconda, all I need to do is conda install python.app and then use pythonw to run all scripts. I hope you also find the solution to your particular case from the FAQ.
Overall, it's a Matplotlib problem, so upgrading (or reinstalling) and trying with different Python versions should get you going.

matplotlib graphs in SPSS

Is it possible to use any other graphing library in SPSS that the built in? I just discovered the python extensions that makes SPSS great.
import matplotlib.pyplot as plt
from numpy.random import rand
fig, ax = plt.subplots()
for color in ['red', 'green', 'blue']:
n = 750
x, y = rand(2, n)
scale = 200.0 * rand(n)
ax.scatter(x, y, c=color, s=scale, label=color,
alpha=0.3, edgecolors='none')
ax.legend()
ax.grid(True)
plt.show()
This will create a simple scatter plot and it works fine in any IDE, but when trying to use that code in SPSS BEGIN PROGRAM END PROGRAM i get the following error:
RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X.
Please either reinstall Python as a framework, or try one of the other backends. If you are Working with Matplotlib in a virtual enviroment see 'Working with Matplotlib in Virtual environments' in the Matplotlib FAQ
Maybe I am asking too much out of the python extension in SPSS but it would be nice to use another graph library than the one they have built in.
Adding more information as another answer as the comment field is too limited.
I do not get the framework error on Windows, which is a different issue, I think. But running your code inside Statistics works - sort of. Instead of placing the image in the Viewer, it pops up in its own window (which may be buried behind another window).
So Statistics doesn't know about it and patiently waits for the program to complete, which doesn't happen until you dismiss that window (which does have a normal frame not shown in the graphic).
To make this work, you would need to direct the matplotlib code to write the image to a file somewhere and then use SpssClient apis to insert that image in the Viewer. See CreateImageChartItem Method (Python) in the Python programmability help. Alternatively, if you can direct matplotlib to write the image to the standard output stream, Statistics might be able to capture it directly in the Viewer.
I usually do programmability images with R code (even though Python is a way better language(!)), where this all works seamlessly. Or I use Python code to write Statistics graphics commands and GPL to have the Statistics engine, which is pretty powerful, do the charting.
Another thing you mind find helpful if you are into Python, is that you can run Python in external mode, where you start with Python code running from your IDE or a Python command line and then have it invoke Statistics by running
import spss
This has great advantages for developing and debugging Python code, but you can't use the SpssClient module methods directly. I ran your code from my IDE (Wing Professional), and the image window popped right up. And, of course, you can use the spss module and related apis in external mode to communicate with Statistics and control it.
You can use just about any Python code or library, but you need to do some configuring. When the Python support (Python Essentials) is installed, it installs a private, unregistered Python system in order not to conflict with any other Python that might be installed. So if you try to add other libraries, the installer doesn't know what to do or installs them somewhere that the Statistics installation won't know about.
The easiest way to get around this is to install another, standard Python installation (version 2.7 in recent versions or 3.4 with version 24 if you need Python 3). Then go to Edit > Options > Files and point to that distribution. You will need to restart Statistics for that to take effect. I use the Anaconda distribution, which includes a lot of other goodies.
I'm not a Mac user, but something like this should work.

emacs-jedi does not find numpy completions

I installed emacs-jedi to get some code completion for python in emacs. In general, I must say that I am very impressed! It works well out of the box and finds completions quickly for built -in libraries. However, I use python for scientific purposes and rely on numpy and scipy for my work. For some reason, I get no completions for these modules.
Example:
import numpy
testVector = numpy.array([1,2,3])
now typing testVector. and waiting, nothing shows up
I wonder why it does not work. It looks like sys.path problem but it should work without any configuration. But here is some idea for a brute force fix.
(1) Run the following script to get load path for numpy.
import os
import numpy
print(os.path.dirname(os.path.dirname(numpy.__file__)))
(2) Set jedi:server-args like this to forcefully add the path.
(setq jedi:server-args
'("--sys-path" "THE/PRINTED/PATH/FOR/NUMPY"
"--sys-path" "THE/PRINTED/PATH/FOR/SCIPY"))
See also: http://tkf.github.com/emacs-jedi/#jedi:server-args
Edit 1
Reading your comment on #syohex's answer, it looks like you mixed up some installation methods. jeid.el uses the virtualenv "env/" in the directory in which you have jedi.el, if it exists. el-get automatically creates "env/" if you have virtualenv. So, if you like system installation, you need to tell Jedi.el to ignore "evn/" by doing this:
(require 'jedi)
(setq jedi:server-command (list "python" jedi:server-script))
See also: http://tkf.github.com/emacs-jedi/#jedi:server-command
Edit 2
I have no clue why is that happening from your description. Here are several ways to narrow down the problem.
Run make tryout in the directory in which jedi.el is installed (like ~/.emacs.d/el-get/jedi/).
This opens a clean (i.e., it does not read your setup) Emacs process with minimal setup for jedi.el. Let's see if you can complete numpy and scipy.
Can you import numpy and scipy in Emacs? You could have different environment variable in Emacs and shell. Run M-! python -c 'import numpy' RET. If this does not give you an error, then it is fine.
Can you import numpy and scipy using env/bin/python? The best way to do it is to check it from Emacs.
So first go to the directory in which jedi.el is installed (e.g., C-x C-f ~/.emacs.d/el-get/jedi/ RET).
Then run M-! env/bin/python -c 'import numpy' RET. If this does not give you an error, then it should be possible to import numpy and scipy.
I hope at least one of them gives you an error, otherwise I need to think about another possibility.
I can get completion such case. Like following
You may use old requirement modules(jedi, epc, argparse).
You should update them and try again.

Setting up eclipse for maya 2013

I followed this instructions:
http://www.luma-pictures.com/tools/pymel/docs/1.0/eclipse.html
Also have read the Maya's documentation:
http://download.autodesk.com/global/docs/maya2013/en_us/files/Python_Python_from_an_external_interpreter.htm#
And now I can successfully import and initialize Maya Standalone and
Cmds module.
BUT, as I try to code something like 'cmds.polyCube()', first I don't have any
auto completion and secondly Eclipse returns with an error saying that cmds module
Doesn't has any variable that named polyCube() etc....
Here is my exact procedure which I use to import and initialize maya inside Eclipse:
import maya.standalone
maya.standalone.initialize()
import maya
from maya import cmds
cmds.polyCube(n='cuby_01')
cmds.select('cuby_01')
Are you sure you followed this step from the pymel eclipse docs:
Click the “New Folder” button again, and add the site-packages directory you removed earlier. We did this in order to ensure that the
stub maya package is found before the real maya package. When you’re
done, the main site-packages directory should be somewhere below the
extras/completion/py folder you just added.
That is the important part for adding the stubs to your custom interpreter. In the end you should have a mayapy interpreter set up, with this added site-packages location. And also, make sure when you create a new pydev project, that you go into its specific properties and set the python interpreter to the mayapy that you set up. Otherwise you could still possibly be using the default python interp.
I wrote a little explanation here, how to add a mayapay interpreter here:
Eclipse environment for Maya's python modules
I'm pretty sure is this one your problem.
give a look and if you have any question don't hesitate to ask here again :)
You can also take a look at this tutorial:
http://www.creativecrash.com/tutorials/using-eclipse-as-a-maya-ide (the most relevant portion is available on the 'page 2' tab in the linked page). Basically, you need to point Eclipse's 'predefined' at the /devkit/other/pymel/extras/completion/pypredef
Your sample should work correctly as long as (a) you're using a 2.6 interpreter and (b) you've got the maya python directory in your eclipse PYTHONPATH:
Its usually easiest to configure eclipse to use /bin/mayapy.exe as the intepreter for maya. You might find it easier to use
import maya.standalone
maya.standalone.initialize()
import maya.cmds as cmds
pc = cmds.polyCube()
the cmds module imports as empty UNLESS you've already initialized maya.standalone - your sample shows that but perhaps you got that error in an earlier run without standalone.initialize()?

Categories