1

I have three scripts in the same location:

 /__init__.py
 interface.py
 radio.py
 playlist.py

playlist.py has child classes stored, like:

class playlist1(radio):
 '''child class'''

and radio.py has the parent class:

class radio(object):
 '''parent class'''

I'm trying to run interface.py, which has:

if __name__ == "__main__":
 from playlist import *

in playlist.py I have this import, on its turn:

from radio import radio

but when I run interface.py, I get the following error:

ImportError: cannot import name radio

I use python 2.x. what's wrong?

asked Jan 1, 2017 at 19:04
1
  • I can't reproduce the problem using just the setup you've shown here. If I create the three files you showed, with the contents you showed (plus empty __init__.py), I can run interface.py without errors. I suspect there is something else going on in one of the files, or perhaps you have another file called radio.py somewhere else that is being imported. Commented Jan 1, 2017 at 19:49

2 Answers 2

1

Your description of the situation has omitted a crucial part: the package where these modules live. For example, if they live in the foo package, the situation would look like this:

foo/
 __init__.py
 interface.py
 radio.py
 playlist.py

In that context, there are two common ways for the playlist module to import names from the radio module:

# 1. Fully qualified.
from foo.radio import radio
# 2. Relative import.
from .radio import radio

The second approach is strongly recommended because it leaves no room for ambiguity.

You also haven't told us how you are running interface.py. Those details can affect the importing situation as well.

If you are organizing code in packages, you need to follow a conventional project structure. In this layout, you would tend to work in the project root. Also you need a proper setup.py file. Here's what it might look like:

# ----
# Directory layout.
some_project/
 foo/
 __init__.py
 interface.py
 playlist.py
 radio.py
 setup.py
 # You work at this level.
# ----
# interface.py 
from playlist import radio
def main():
 print radio
# ----
# playlist.py
from .radio import radio
# ----
# radio.py
radio = 123
# ----
# setup.py
from setuptools import setup
setup(
 name = 'foo',
 version = '1.0',
 zip_safe = False,
 packages = ['foo'],
 entry_points = {
 'console_scripts': [
 'foobar = foo.interface:main',
 ],
 },
)
# ----
# Install stuff for dev work (ie in "editable" mode).
pip install -e .
# ----
# Run the command line entry point.
foobar
answered Jan 1, 2017 at 19:26
Sign up to request clarification or add additional context in comments.

3 Comments

isn't dotnotation for python 3.x? mine is 2.x. If I do what you say, I get ValueError: Attempted relative import in non-package. all files are at root.
If you are organizing code in packages, you need to follow a standard project structure. Putting everything in the root directory and running code from the root won't work well for various reasons -- starting with the fact that Python won't interpret the root directory as a package. There are various resources on this topic, but here's one: jeffknupp.com/blog/2013/08/16/…
@data_garden I added some more details on project structure
0

I think you just need to make an empty file called

__init__.py 

in the same directory as the files. That will let Python2 knows that it's okay to import from this directory. Then use your code.

answered Jan 1, 2017 at 19:13

2 Comments

it's already there, I forgot to mention that and have done it now on my edit. sorry.
By any chance do you have circular imports?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.