Adding python modules to pydev in eclipse results

2020-02-08 06:03发布

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 command

python --version

shows Python 2.6.6.

import unidecode

and

from unidecode import unidecode

work both fine!

I installed PyDev in Eclipse and went to configured the python interpreter (Auto Config). I selected all the proposed packages and hit Apply and Ok.

But eclipse keeps complaining

Traceback (most recent call last):
  File "/Users/me/Documents/workspace/myproject/python/pythontest.py", line 12, in <module>
    from unidecode import unidecode
ImportError: No module named unidecode

The python file looks like this

#!/usr/bin/env python
# encoding: utf-8


import sys
import os
from unidecode import unidecode


def main():
    print unidecode(u"Ågot Aakra")

if __name__ == '__main__':
    main()

When I remove the first line in the script

#!/usr/bin/env python

it results into the same error.

Does someone know where the problem lies?

11条回答
不美不萌又怎样
2楼-- · 2020-02-08 06:16

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.

查看更多
一夜七次
3楼-- · 2020-02-08 06:17

You can simply add the module to the pydev path. Go to project properties (from the context menu) -> PyDev -> PYTHONPATH -> External Libraries. Depending on whether the module is in a source folder or a zip/egg file, select either Add source folder or Add zip/jar/egg. Navigate to the site-packages directory and point to the relevant file or folder (mine is: /usr/local/lib/pythonx.x/site-packages)

查看更多
Animai°情兽
4楼-- · 2020-02-08 06:22

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.

查看更多
兄弟一词,经得起流年.
5楼-- · 2020-02-08 06:22

@Aufwind your answer above helped but didn't solve for me.

  • Find the path to the folder ../site-packages/ ....
  • Open eclipse preferences and go to PyDev -> Interpreter - Python.
  • On the left side of the lower box, click on New Folder. ---> here I departed from your instructions. I added the egg for the module that wasn't being recognized. Adding the site-packages folder didn't fix it.
  • Hit open.
  • Hit Apply.
  • Hit Ok.

And then I was good to go. =)

查看更多
来,给爷笑一个
6楼-- · 2020-02-08 06:22

Suppose your eternal module is in /.

Launch Eclipse and go to the project option. Select "PyDev-PYTHONPATH" and on the right you will see a tabbed window. Select External Libraries there. Click on Add Source Folder and select your library from the above path.

查看更多
走好不送
7楼-- · 2020-02-08 06:25

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

查看更多
登录 后发表回答