Adding python modules to pydev in eclipse results in import error?

前端 未结 11 2093
难免孤独
难免孤独 2020-12-14 03:01

I have a problem getting PyDev on eclipse to recognize already installed modules. Here is my detailed approach. The machine is a Mac (Snow Leopard).

In terminal the

相关标签:
11条回答
  • 2020-12-14 03:20

    This is the solution to my problem:

    1. Find out the path to the folder ../site-packages/ of your corresponding python version. ( For me it was /opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/ for python 2.6 on my Mac with Snoe Leopard.)
    2. Open eclipse preferences and go to PyDev -> Interpreter - Python.
    3. On the left side of the lower box, click on New Folder.
    4. Add the navigate to ../site-packages/ of your corresponding python version.
    5. Hit open.
    6. Hit Apply.
    7. Hit Ok.

    And you should be good to go. =)

    Thanks @all particionts, who provided hints into the right direction in the comments.

    0 讨论(0)
  • 2020-12-14 03:25

    In my case I was not getting this error before compiling, but when I compile I got the error ImportError: No module named myant.core. I tried to add the files from PyDev-PYTHONPATH, but again I got the same error. Then I realized that I actually do not have to add the path exactly to the folder where my .py files are located. Infact I have to add the folder where myant.core is located. After doing this I did a restart when I recompiled my project again, the problem was fixed. I would share that I have:

    Python 2.7 Eclipse kepler 4.3, PyDev 3.9.2 and on my ubuntu 14.04

    .py files location:/${PROJECT_DIR_NAME}/src/myant/core, therefore I added /${PROJECT_DIR_NAME}/src

    0 讨论(0)
  • 2020-12-14 03:28

    I ran into the same problem just today. I am using pydev and had a working project with a number of sub-packages. Suddenly after having created a new module I was not able to use this module in a different package. The puzzling feature was that I could use another module in the same sub-package...

    Finally after

    1. eclipse restart
    2. remove/add python interpreter and all site-packages
    3. annoyed head-scratching

    I deleted all compiled classes with the following script:

    import os
    
    def clean_folder(folder):
      for file in os.listdir(folder):
        path = os.path.join(folder,file)
        if os.path.isdir(path):
            clean_folder(path)
    
        if '.pyc' == file[-4:]:
            print 'deleting: ' + str(path)
            os.remove(path)
    
    if __name__ == '__main__':
      folder = 'YOUR_PROJECT_SRC_PATH'
      clean_folder(folder)
    

    and finally I can do 'actual' work :) Hope it helps somebody...

    0 讨论(0)
  • 2020-12-14 03:28

    When Eclipse gets 'lost' with respect to what packages exists on your system or in your project, from the context menu of your project, choose 'Properties' menu item, then the 'PyDev - PYTHONPATH' item in the treeview on the left of the dialog, then the 'Force restore internal info' button. Seemingly, PyDev keeps a computed cache of the info and when for any reason the cache becomes incoherent, you can force PyDev to recompute.

    0 讨论(0)
  • 2020-12-14 03:28

    Try preferences > pydev > interpreter - python and removing and re-adding the python interpreter (make sure you know the path to it before you delete it), when you re-add it tick all the boxes.

    0 讨论(0)
  • 2020-12-14 03:29

    For Oxygen 2 (I think it worked on earlier versions, too)...

    1. Right click on project folder and select "Properties"
    2. Select "PyDev - Interpreter/Grammar"
    3. Click on "Click here to configure an interpreter not listed"
    4. Select any existing interpreter from the top list of configured interpreters
    5. A "Selection Needed" dialog should appear where you must select one or more interpreters to restore. Check all that apply
    6. Click "Ok" and PyDev will rescan, and I assume, rebuild some internal view of your site-packages
    7. Click "Apply and Close" to close all dialogs

    To make the import error markup disappear in my code editor, I need to type a space after the offending import then save the change. The import error then disappears because PyDev can now find the offending import module.

    0 讨论(0)
提交回复
热议问题