Python with eclipse import problem - python

I use eclipse as my IDE for python. Recently I encountered a strange problem.
A file contains an import from external libraries (such as wx, matplotlib etc.) if I put it inside the src directory, it will run as expected, but in the editor, I get error marks all over the places where I use the imported libraries. The errors say things like "Undefined variable from import".
If I put it in top level of the project, the error marks disappear.
I have added a screenshot of the configuration. I have a feeling that the source of the problem is there, but do not know what to do.

In my installation I have several other libraries in the system pythonpath dialog including wx. Try setting the interpreter again in order to reload the libraries or load them manually.

Related

The red underlines (unresolved references) in PyCharm/IntelliJ stop working - on some of my Anaconda projects

I'm using the PyCharm (Python plugin) on IntelliJ Ultimate.
Normally the IDE highlights any undefined identifiers with a red-underline, but I've noticed that on some projects this feature stops working.
Red-underlines are really useful because they allow me to use IntelliJ's auto-fixes (e.g. importing or creating a missing function/class).
Just about every other PyCharm feature I can think of seems to still be working. For example, AutoComplete works just fine.
Sometimes if I set the project's SDK to , and then set it back to the correct Python interpreter the project starts to highlight NameErrors correctly. This doesn't seem to work consistently. I'm using Conda, and I've tried setting up Conda envs as normal virtualenvs, or as "system interpreters".
I've searched the log files and cannot see anything that might indicate the plugin failed to initialize.
In most cases (except for when the system briefly decides to work), I get no red underlines but all of the other PyCharm features seem to work normally. Furthermore, this only seems to affect Python: Scala and Java code behaves exactly as you might expect it to work in IntelliJ.
Is there a way to debug this? What's going wrong? How do I fix it?
I've had this same problem with the symptons you describe. From the description the cause is that you have "Reader Mode" enabled.
Normally the IDE highlights any undefined identifiers with a red-underline, but I've noticed that on some projects this feature stops working.
The red underline/scribble for errors is suppressed for modules that are currently installed in the projects virtual environment or interpreter. (This makes sense because if for some reason you want to read code from an installed library it's usually not your concern if the library author made mistakes or if that library code for example issues warnings according to your code style settings, etc...)
Sometimes if I set the project's SDK to , and then set it back to the correct Python interpreter the project starts to highlight NameErrors correctly.
If you change the project SDK to an interpreter that does not have your modules installed the errors will be shown again. You can set the display of errors with Reader Mode enabled checking the option at File > Settings > Editor > Reader Mode > Error and warning highlighting, inspection widget. As shown in the screenshot
If you disable Reader Mode the IDE will also again issues errors as usual.

No module named <packname> - PyCharm

I have searched for the problem on the site, but could not find the case or a similar one. This one I checked also as the best matching
tihs one.
I am using the system interpreter, Python (v3.7.0), and I can access all the packages in the python IDE no import error or something like that. In fact, when I use PyCharm and try to use import statement in the script, it shows 'No module named ', and when I run the script on PyCharm or from a different source it works with no error. In other words, I have a problem only in IDE, and since PyCharm IDE shows an error that there is no module when I am scripting, I cannot use the PyCharm IDE things associated with that module such as auto-complete. Yet, when I run the script from any shell or IDE they all successfully run the codes.
I don't figure out what is wrong, but I will provided some images below.
I have been using PyCharm for the projects, lately I've probably done sth wrong, but not sure so that PyCharms throws an error in IDE while scripting. The thing is while scripting when I use import statement PyCharm does not recognize the module which is already installed.
I don't figure out what is wrong, but I will provided some images below.
1st
2nd
3rd
Did you checked the Project Interpreter? If the interpreter you are using is different from IDLE, this might happens. Go to Preference -> Project Interpreter, and check the interpreter you are using it now. You can also check the packages that are currently being installed right away.

Where do I place or how do I install my own Python module

Sorry for the super-simple question, it's just that I haven't found a proper answer here (that worked for me).
I've just wrote some demo Python code, a Python module (logically speaking) to be exact, and I want to import this and use it.
Where should I place my module_x.py file? (I'm running on Windows)
I've checked that all the module .py files are placed under ..\Python27\Lib directory, but placing my file there doesn't work for me –
when I try to run import module_x I get this error message:
ImportError: No module named module_x
The simplest thing is to put it in the same folder as the script that uses it.
If even this does not work, check to see if Windows has snuck in a file extension such ".txt" that isn't visible in the way you are viewing the file name.
As you have time, please read how to deal with modules from a manual or book to learn to do it the right way.

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.
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.
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.
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.

Is there a way to suppress unresolved imports in eclipse in a PyDev project?

I have a Python project in eclipse, which imports modules that can't be found by Python. Here's a list of some of the cases:
some of the files might import both the 2.x and 3.x versions of some built-in modules for compatibility purposes (but I can specify only one grammar version in the project's settings)
since the scripts I'm writing will be ran in an environment very different from mine, some of the modules I use don't even exist in the system (like Windows-specific modules, or modules from other projects that I REALLY don't want to link directly to this one)
modules that might or might not be installed on the machine where the script is going to be executed (of course, wrapped into try-except clauses)
and so on...
It is very annoying to have these modules marked as errors, since they make REAL syntax errors much less noticeable.
I know that this behavior can somehow be overridden - I have another project that doesn't mark unresolved imports as errors, but I just can't find the right setting for it. Can anyone help me?
How about adding ##UnresolvedImport to your imports? Eg:
import a_module_pydev_doesnt_know ##UnresolvedImport
You can simply press Ctrl-1 when your cursor is placed in a line where PyDev marked an error and and select the corresponding entry to automatically add it.
Edit: I don't have much experience with it, but it seems that if you want to change this for a whole project (or do it without touching your code), you can also add the module in question to the forced built-ins: http://pydev.org/manual_101_interpreter.html#PyDevInterpreterConfiguration-ForcedBuiltins

Categories

Resources