简体   繁体   中英

Python import requires module name

I have two .py files in the same directory on PyDev in Eclipse like so:

DemoProject
|
+---- __init__.py
|
+---- demo_functions.py
|
+---- demo_runner.py

In the demo_runner.py file, I attempted to import demo_functions using

import demo_functions

but PyDev labels this as a problem import. It likes

import DemoProject.demo_functions

though. The project runs even though PyDev doesn't like it. Any idea what's wrong with PyDev?

Doesn't sound like anything is "wrong" with it per se. You should be using full/correct imports and not hoping that the interpreter knows what you're talking about, especially since newer versions of Python only support proper imports .

from . import demo_functions

The issue is that you should have added 'DemoProject' as your source-folder in PyDev (this is the folder to be added to the PYTHONPATH) instead of letting the project base directory in the PYTHONPATH.

You can change that at your project configuration (select project in the pydev package explorer > alt + enter > pydev - pythonpath)

As a note, you probably don't want the __init__.py at that level (since that's your top-level and not a package).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM