y3trgfhsfgr
y3trgfhsfgr

Reputation: 467

Inconsistent Import Error in Python

I just have a quick question about an error I've been getting when I try to import:

from psychopy import gui

So if I try to import this code in one program I have no problems, however, if I try to import it in another I get:

"ImportError: cannot import name gui"

Does anyone know why this might be happening? Why does it work for one problem, but not the other? Also, I feel like it wasn't doing this before, and it just started suddenly. Any advice would be greatly appreciated.

Upvotes: 2

Views: 326

Answers (3)

Jonas Lindeløv
Jonas Lindeløv

Reputation: 5683

UPDATE: I think Jon's answer is the correct one. If I was right, you should get an error "no module named psychopy".


Given that you tagged this question with the psychopy tag, my guess is that it works if you run it from the psychopy app and that it doesn't work if you run it from another editor or command line.

The reason is that psychopy is currently shipped as a bundle that comes with it's own python and a lot of modules/dependencies, including psychopy. The system is not made aware of these modules via the PYTHONPATH.

You can make them available system-wide by either (1) following the steps outlined here or (2) use the conda based installation described in this post in the psychopy-dev list. The latter feature is still work in progress but will probably eventually mature to be the default install option.

Upvotes: 2

Jon
Jon

Reputation: 1223

I think the other answers are wrong ;-) I think if you had a different virtual environment or installation then the error in your code would indicate "No module named psychopy"

The fact that it finds something called psychopy but no sub-module called gui is a different problem. Often this occurs if you have a folder or file called psychopy next to you current working directory (eg. next to where you launch the script). Then Python thinks that's the psychopy module but can't find gui within it.

So, do you have a folder called psychopy? Rename it psychopyStuff.

Upvotes: 2

Anshul Goyal
Anshul Goyal

Reputation: 76907

I think you are using different virtual environments for both the projects, and so the package is installed in one virtualenv, and not in the other.

To verify this is the case, do a pip freeze in both the projects and compare the results.

If there is a single environment, the output will be same, otherwise the outputs will be different amongst the two.

Upvotes: 1

Related Questions