related to:
Unresolved Import Issues with PyDev and Eclipse
Recently I moved my django package to django_trunk so I can have a development version elsewhere. I have no issue importing django, runserver, or any of that.
My issue (annoyance?) is that all of the imports are coming up as underscored red indicating an error.
Pydev clearly knows of the imports, but for whatever reason it still imports as red.
I've noticed this with a few other packages, which use a .pth file instead of a direct import. In the past I've just ignored the red, but with django it's half my screen and I'm loosing critical information.
To demonstrate:
import django #note that django is underscored as red
import sys
print django
print sys.path
module 'django' from 'C:\Python27\Lib\site-packages\django_trunk\django__init__.pyc'
[...,'C:\Python27\Lib\site-packages\django_trunk', ...]
Try reconfiguring the Interpreter by going to Window -> Preferences -> PyDev -> Interpreter-Python and removing and adding the Python interpreters again.
Related
I have a weird phenomenon (Windows, python 3.8.5): I have a folder: c:\myfolder. Somehow this folder got into the sys.path_importer_cache, and now every subfolder in c:\myfolder can be imported as namespace packages.
So for example, there is this subfolder: c:\myfolder\foo\
And if I try to import foo, I get this:
import foo
foo
<module 'foo' (namespace)>
foo.__path__
_NamespacePath(['c:\\myfolder\\foo'])
and its loader is a NamespaceLoader.
The problem is, that foo itself is a package, that is installed into the site-packages, and now it does not get imported from there, because this weird namespace package beats it.
What have I messed up? Where do I look? I could only find this hint, that c:\myfolder is in the sys.path_importer_cache.
But why did it get there? How can I remove it? (I tried importlib.invalidate_caches(), no success...)
One more hint: if I run python in isolated mode (python -I), this problem goes away. However, I don't have anything in my user-site that could cause it (as far as I know.)
Any ideas? Thanks!
It has been figured out... I python in Anaconda, and I have launched the Anaconda Prompt (with condabin/activate) in the c:\myfolder as start folder. That's why it got into the path_importer_cache... If I start it simply in c:, the problem goes away.
I've been working on a Django project for months, all has been fine, then I did a big code merge and PyCharm has stopped recognizing module imports. The following work:
from django.utils.timezone import *
from django.test.testcases import TransactionTestCase
from kimsim_app.views import *
But these no longer do:
from django.utils import timezone
from django.test import TransactionTestCase
from kimsim_app import views
By work, I mean the latter timezone and TransactionTestCase are highlighted with red underlining. Lines of code that are missing imports are not underlined (although perhaps the parser gives up). Optimize imports does nothing. Update: a few minutes later and testcases import TransactionTestCase is again highlighted in red (edit - working again).
I have restored an old .idea directory from before the problems. I have recreated from scratch the virtual environment. I have duplicated the source, removed .idea and recreated the PyCharm project - nothing seem to help, yet running the Django unit tests from the command line work fine. When I navigate to the source of django/utils/__init__.py the code is not syntax highlighted. I have tried setting the settings file in Run Configurations and 'Settings, Languages and Frameworks, Django'. I have tried upgrading to PyCharm Pro 5.0.4. A colleague with identical source code has no problems.
Only when I uninstalled, deleted all the directories listed on this page and below, then reinstalled, did the import validation start working again. On this latter attempt I did recreate the PyCharm project (ie, the .idea directory), I did not enter my license code for fear of it downloading saved settings or something, I did not recreate the virtual environment or VCS settings.
The directories listed for OS X:
Configuration: ~/Library/Preferences/<PRODUCT><VERSION>
Caches: ~/Library/Caches/<PRODUCT><VERSION>
Plugins: ~/Library/Application Support/<PRODUCT><VERSION>
Logs: ~/Library/Logs/<PRODUCT><VERSION>
For Unix systems (I'm not sure if this bug affects Unix or Windows): ~/.<PRODUCT><VERSION>
Windows Vista, 7, 8: <SYSTEM DRIVE>\Users\<USER ACCOUNT NAME>\.<PRODUCT><VERSION> eg, c:\Users\Bert\.PyCharm45\
Windows XP: <SYSTEM DRIVE>\Documents and Settings\<USER ACCOUNT NAME>\.<PRODUCT><VERSION>
In addition to the above, I also tried uninstalling all versions of PyCharm then reinstalling, creating new everything (virtual env, .idea project settings, etc) except the source code, but I still got the error - only when I deleted all the directories listed did the bug disappear. I haven't dared re-import my settings. The jetbrains bug hasn't been assigned after two days.
When I try to import time I get : No module named time
I have tried other time modules(datetime and timeit) and they work fine. I decided to check my installation and I can't find time.py anywhere. I checked the Lib, Scripts, libs and include folders, but can't find it anywhere.
Anyone know what I can do to fix this? Maybe download the .py and put it in Lib myself?
I am using Python 3.3.5 with PyCharm IDE. Only extra scripts I've installed is EasyInstall and PRAW.
The import does work. When PyCharm said No module named time, I assumed I would get a compiler error and started trying to fix it.
However when I eventually just ran the code it worked fine. I expect PyCharm doesn't detect the time module as it's a dll and not a py as noted by Martijn in the comments. This is on PyCharm Community Edition 4.0.4.
I tried playing with virtualenv and a host of other things, but I eventually went to Preferences -> Build, Execution, Deployment -> Console -> Python Console, and in the "starting script" box, I added two lines:
sys.builtin_module_names.append('sys')
sys.builtin_module_names.append('time')
This got rid of errors I had with both sys and time. Once I did that, I even get autocomplete for both of those modules... weird.
In pydev I have a python package called webcrawler. This package is in the directory '/home/raido/Workspace/WebCrawler' The package contains a number of modules; website, tier, referrer, etc. etc... Each module contains a series of functions. I wanted to use one of these functions in another pydev project so I typed....
import sys
sys.path.append('/home/raido/Workspace/WebCrawler')
from webcrawler import website
print website.getXmlLang('http://www.google.com')
The script runs fine and runs the function that prints out the information. What I don't understand is why the word website in the "from webcrawler import website" line is underlined in red. The error says...
Unresolved import: website
website Found at: TestUrl
from webcrawler import website
However, everything appears to run fine. Is this a pydev bug? How do I fix this? I tried doing it this way.
import sys
sys.path.append('/home/raido/Workspace/WebCrawler')
from webcrawler.website import getXmlLang
print getXmlLang('http://www.google.com')
Even though this also works doing it this way also underlines the import "getXmlLang" in red.
Python 2.6.5
Eclipse 3.7.1
PyDev 2.5.0.2012050419
Ubuntu 10.04
You should add all libraries used by your project in PyDev - PYTHONPATH/External Libraries tab which you can find in project's properties. This should solve the problem.
I've got a PyDev project that uses protocol buffers. The protocol buffer files are located in a zip file generated by the protoc compiler. Everything works when I run the program, however PyDev reports "Undefined variable from import" for every enumeration constant. So for example:
import model_pb2
value = model_pb2.Expression(type = model_pb2.Expression.PARAMETER)
It reports the enum constant "PARAMETER" as being an undefined variable. There are several dozen similar errors in my program, and I'd like to fix them "properly" (i.e. not simply suppressing the warning.)
I found that using for builtins as can work, but only if all the proto files are in a separate located in an external library (see http://pydev.org/manual_101_project_conf2.html).
This should work:
Move (or unzip) the compiled proto files including model_pb2.py into a directory outside the pydev project.
Add an empty __init__.py file to the same directory as model_pb2.py to ensure it can be imported as a library.
In eclipse, go to Windows -> Preferences -> pydev -> Interpreter
Add the directory with model_pb2.py to the Libraries.
Add model_pb2 to the forced buildins.
If you're not addicted to autocomplete, you may using ctrl+1 to ignoring these errors instead as described in this answer. This was tested with Eclipse Kepler and pydev 2.8.
I encountered this issue with protobuf 2.6.1 and PyDev 4.5.5. I tried the suggestions above both none of them helped in my case. What ended up getting rid of the 'undefined variable' errors when using protobuf enums was simple:
Access the enum on an instantiated protobuf object rather than on the protobuf module.
I'm not sure if this could be applied to the OP's use case, but in mine it was as easy as:
from myprotobuf_module import SomeProtobufMessage
some_protobuf_object = SomeProtobufMessage()
some_enum = some_protobuf_object.SOME_ENUM
Have you tried adding "model_pb2" to your forced builtins? See: http://pydev.org/manual_101_project_conf2.html for details.