No autocompletion using Spyder

As proposed by @lindeloev at http://lindeloev.net/?page_id=216, I use Spyder to prepare the experiment’s script. My operating system is Windows 7 (64bit). The installed versions are:

Python 2.7.12 |Anaconda 4.1.1 (32-bit)
IPython 4.2.0
PsychoPy 1.84.0

The specific modules corresponding to PsychoPy are imported as follows:

from sys import path 
path.append("C:\Program Files (x86)\PsychoPy2\Lib\site-packages\PsychoPy-1.84.0-py2.7.egg")
path.append("C:\Program Files (x86)\PsychoPy2\Lib\site-packages")

from psychopy import visual, core, event, gui, data
from psychopy.tools.filetools import fromFile, toFile

Unfortunately, the autocompletion in the Editor does not work. Furthermore, the Object inspector yields no information about definitions used in the Editor. There are no problems concerning other modules (e.g. numpy, matplotlib)

Inserting my PsychoPy commands into the IPython console, everything behaves completely normal.

Any ideas what the problem may be?

You’ve added psychopy to the path inside your script but you haven’t told Spyder about it. Spyder needs to find the psychopy modules in order to interrogate them I think.

BUT I would also recommend you don’t ultimately run your experiment from Spyder; all the nice things it does in terms of analysing your code, and checking for debugging points as it runs, come at a performance cost. PsychoPy’s own editor has fewer features but will run your experiment faster too.

1 Like

Thanks for your reply. After adding the paths to the PYTHONPATH manager of Spyder, everything is fine.

Concerning your remark, I am not planning to run the final experiment with Spyder but some functions (e.g. Variable explorer, Object inspector) are quite helpful preparing the script.

2 Likes

Completion works with a minor drawback. See this example:

After

from psychopy import visual

in a Jupyter console you can do ‘visual.MovieSt’ and get completion. In the Spyder editor this does not work for the MovieStim*. If you want them to work there, you have to

from psychopy.visual.movie3 import MovieStim3 # or
from psychopy.visual import movie3

to be able to do MovieSt<TAB> or movie3.MovieSt<TAB> respectively. As mentioned in this github issue, this is because spyder 's autocompletion only recognizes classes/modules correctly if they were imported in the subpackage’s __init__.py file. A look at https://github.com/psychopy/versions/blob/master/psychopy/visual/init.py confirms that only those modules explicitely imported work and none of those imported with psychopy.contrib.lazy_import.

It’s not that much of a hassle - do you think there might be a simple solution to get lazy_imports to work with Spyder’s introspection? Or should it be the other way around?

1 Like

I have no idea I’m afraid and supporting Spyder is not on my priority list at all. Lazy importing is a method we’ve taken to speed up loading of the visual module by only importing things when they’re actually used. I suppose there could be a PsychoPy preference to turn this off and revert to the old import-everything code if someone wanted to write that (default should be lazy_import_modules=True)