I use intellij with python plugin.
when I want to import python libs like
import random
I got editor error.
No module named random less... (Ctrl+F1)
This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items.
when I run the code every thing is ok
what can I do to make the intelij recognize this libs?
You may have fixed this by now, but I was having the same problem and finally solved it, so I figured I'd post the solution for anyone who came here by Googling/Binging the error message:
I went to File > Project Structure > Modules, highlighted the main module, then pressed the plus sign and then "Python" under "Framework".
Hope that helps you or someone else.
may be my intellij version is different with you guys.
on Windows platform I fix this problem by:
1.File > Project Structure > Modules
2.on the module's dependencies panel,change the module SDK from JDK to python
3.done
Related
I followed a tutorial for websockets in python and stumbled across the issue that pylance does not reccomend me the fuctions related to the class that I have imported from a module:
My Editor:
The Tutorial:
The Code Itself runs without any issue, so the Import seems to work, but I dont recieve the reccomendations in vs code. What is the reason for this or where could i debug something like this?
Thanks to wjandrea I found some thing I personally did not stumble acros before.
So a classic mistake happened to me when following a tutorial, I work on newer versions than the 1.5 year old video... Unfortunately the part talking about versions was in a nother part...
Long story short, in the mean time a bigger change for the websockets module appeared since the used functions are now imported lazily. Wich makes sense to reduce startup time incase you'll run a websocket server with the module.
Little info about lazy imports (for me it was the first time i heard about this intelligent feature)
Incase anybody else stumbles across this im currently on Python 3.10.7 and I am talking about websockets 10.3!
Back to the issue.
Pylance obviously can't make any reccomendations since the functions like websockets.connect(uri, ...) are just loaded if they are used in the runtime by the default websockets module so tools for code reccomendations inside the editor dont not know they are there..
I took a glance inside the module and through the indirect hint from wjandrea about the lazy imports inside __init__.py the listed dictionary made much more sense now! Based on this I could backtrack the Python scripts I need for my functions or rather Pylance needs to create those handy reccomendations for me inside vs code (or any other ide).
For now I just manually imported the desired script so I have a bit more guidance while writing and since startuptime in my current project is not crutial I let the manual imports exist or i´ll just change out the import variations based on the cirumstance if I am currently developing or if the code goes into production.
I am able to refactor variables in VScode, but I would like to refactor the module names so that the import of those modules are automatically resolved.
A similar feature is present in JetBrains Pycharm but I could not find in VScode.
Such a feature doesn't exist in the Python extension for VS Code. To request it, open an issue at https://github.com/microsoft/vscode-python .
Update: This issue has been raised in issue 4628. The feature still doesn't exist for now, but hopefully it will soon.
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.
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
I followed this guide to setup OpenCV 2.3.1 in Python 2.7 with Eclipse.
I also copied the libraries into my python folder:
http://i.snag.gy/J9RrC.jpg
Here is my Hello World program which runs correctly (creates a named window and displays the image) but Eclipse still shows syntax errors
every error says "Undefined variable from import"
Here are my python settings for this project:
http://i.snag.gy/KBXiB.jpg
http://i.snag.gy/KfTpF.jpg
Have I setup my PythonPath incorrectly? How can i get Eclipse to work properly?
Thanks
I had the same problem, everything ran correctly even though there were undefined import errors all over the place. I eventually solved it by adding 'cv' to the list of Forced Builtins: Window > Preferences > Pydev > Interpreter - Python > Forced Builtins > New.
This is how I came across the solution:
How to use code completion into Eclipse with OpenCV
I hope that this may help you too.
EDIT: FYI, according to the top answer here, if you're just getting started (like me!) it's almost certainly better to use the cv2 interface instead of the older one provided in cv2.cv. The author of that answer, Abid Rahman, has some tutorials that look pretty good. (end EDIT)
I used Debian's tools to install the python-opencv package. There was no .../dist-packages/opencv directory to be found, and the cv.py file contained only:
from cv2.cv import *
I'm fairly inexperienced with Python and completely so with Python access to external libraries, so this looked like some sort of workaround related to that. Not so, apparently. I followed Casper's link above, and found the solution that he used (which worked for me,) but I wasn't happy using "forced builtins" when I wasn't entirely sure of the consequences.
However, the second, lower-rated answer there is my preferred solution. Instead of
import cv
I'm using
import cv2.cv as cv
From what I can tell, this just removes the cv.py middleman from the import chain, if that makes sense. A save/close/reload of my script had Eclipse recognizing cv.LoadImageM as defined and autocompleting other things from OpenCV.
I'm reproducing that answer here because it seems cleaner to me and I found this question first when I searched for the answer to the same problem.
It would be helpful to show the error you're getting and your code. However, I suspect that the problem is that the syntax errors which PyDev shows are based on its own parsing of the code, which is much more simplistic that the actual python interpreter. If your code runs, then the apparently undefined variables must be defined, but the PyDev parser just can't see them and reports them as "undefined".
The cause of this is that OpenCV doesn't explicitly define its variables in a way which can be read by PyDev. Unfortunately I don't have an easy solution. I usually deal with the problem by using from ... import ... so that the error only appears once. If you want you could write a wrapper module which explicitly imports the variables into its local namespace, then import that module instead.