Reputation: 1318
I'm trying to understand what PyDev doesn't like. I'm under Ubuntu and it seems that PyDev doesn't see the libraries like bash does. I have a problem with two libraries, sqlite3 and peewee. If I run my program on a shell, all fine; if I open it in my newly installed PyDev I see a couple of instruction underlined with the message
Unresolved import: sqlite3
And
Undefined variable from import: get
The first error comes from the following code:
from pprint import pprint
import sqlite3
from bs4 import BeautifulSoup
import codecs
from database import Tbrecipe
from datetime import datetime
import logging
def main():
logger = logging.getLogger('peewee')
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())
CONN = sqlite3.connect('ent.db')
The import error happens at the.. import. Second line.This happens because I've personally added /usr/lib/python2.7/sqlite3 to the pythonpath on the pydev's python interpreter. If I remove it, the error is at "sqlite3.connect". I suppose connect is not defined on that directory. I didn't find it.
Peewee has other issues. I created an object class for my database's table. All fine until I try to use some methods inherited from peewee.
My database.py looks like this:
from peewee import * database = MySQLDatabase('test', **{'host': 'localhost', 'password': 'rt', 'user': 'rt','charset':'utf8mb4'})
class UnknownField(object): def init(self, *_, **__): pass
class BaseModel(Model): class Meta: database = database
class Tbitem(BaseModel): source = IntegerField() name = CharField(null=True)
on my main.py
...
from database import Tbitem
item = Tbitem.get(Tbitem.id==id_item)
both "get" and "Tbitem.id" are underlined in red with the error
Undefined variable from import: get
I can continue to work as nothing happens, or I can go back to vim or vscode (slow debugger), but I"d prefer using this since I'm used to eclipse and I like the idea of pydev. What should I do? I did check pythonpath on a shell and it looks the same, other than a directory that doesn't exist anymore.
I read on the FAQ of pydev that it doesn't like softlinks. Should I remove all softlinks? peewee is not softlinked and sqlite3 I don't even know where it finish.
Anyone with similar issues and a solution? I did read most of the questions here on SO, but they don't work for me.
Upvotes: 0
Views: 1236
Reputation: 19
I had the same issue with Orange-Bioinformatics, code would run fine but PyDev showed an unknown import.
The problem in this case were missing __init__.py
files in the Orange-Bioinformatics archive, which pip downloaded and installed. PyDev seems to want a namespace declaration for each module, so I simply created a file in ~/.local/lib/python3.4/site-packages/orangecontrib/ (would be /usr/lib/python2.7/sqlite3/ in your case), named the file __init__.py
and saved it with the following content:
# namespace stub
__import__("pkg_resources").declare_namespace(__name__)
The file was put into the subfolder orangecontrib/bio/
Just refresh PyDev's module list via Window -> Preferences -> PyDev -> Interpreters -> Python Interpreter -> Apply. Now all works well for me :-)
Upvotes: 1
Reputation: 1318
I solved it by forcing the build of all external libraries that caused that error (force builtins under interpreter's properties) For my libraries I deleted all .pyc files and tried again and it worked.
Upvotes: 0
Reputation: 1
Bonjour,
As you, I had an unresolved import only on IDE and I found a workaround.
I installed "netifaces" using the appropriate command :
pip3 install netifaces
After the installation, I got two entries in the folder "/usr/local/lib/python3.6/site-packages" (ref. Mac OS + homebrew).
At this point, I had an unresolved import issue in the PyDev editor, but my application runs well when I launched it using a Pydev launch configuration and the same interpreter.
Note: I also import other modudes installed using pip3 and their importations did'nt generate issue.
I created the symbolic link "netifaces.so" to get the same file I found in the "site-packages" folder of the Python 2.7.13 interpretor
ln -s netifaces.cpython-36m-darwin.so netifaces.so
After this creation, all run well : the import issues desapear in the Pydev editor.
Question : where is the error ?
Upvotes: 0