1

I've read through about ten posts on how to import local modules, and I'm still stumped on why this isn't working. I have an extremely simple module, actor.py, with a single class inside it:

class Actor(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age

I'm trying to import it into another module, scraper.py, within the same directory: enter image description here

Some fixes have listed not having init.py as being a problem with local imports, so I know that's not my problem. Initially I tried these:

import actor

and

from actor import Actor

but it tells me that actor and Actor are unresolved references. here tells me that's Python 2 syntax, and I'm using Python 3. That answer instead recommends that I do:

from .actor import Actor

When I run my program with that syntax, I get this error:

ModuleNotFoundError: No module named '__main__.actor'; '__main__' is not a package

So I go searching again, and this post tells me to remove the dot from 'actor,' but as stated before, I've tried that as well. My final guess was

from . import actor

but that yields

ImportError: cannot import name 'actor'

which I follow to here, but the answers there mention circular dependencies, and I'm certain actor and scraper have none. Am I perhaps not writing my module correctly? I can't think of any other ways to write an import statement.

edit: if it helps at all, I'm using Intellij

lmotl3
  • 537
  • 1
  • 8
  • 20
  • how are you trying to run this? `'__main__' is not a package` suggests a problem with the way you are executing. – avigil Feb 25 '18 at 08:14
  • should be: `from actor import Actor`, You import the class from a file in the same directory. – manandearth Feb 25 '18 at 08:29
  • @avigil I'm running the main method inside scraper.py. – lmotl3 Feb 25 '18 at 14:39
  • so `python -m WebScraper.scraper`? You cannot call it directly with `python scraper.py` if its part of a module. – avigil Feb 25 '18 at 15:14
  • or if you are running inside IntelliJ this is a problem with your IDE run configuration and not the code itself. – avigil Feb 25 '18 at 15:27
  • @avigil I am running inside Intellij, yes – lmotl3 Feb 25 '18 at 15:28
  • I tried this: https://stackoverflow.com/questions/21236824/unresolved-reference-issue-in-pycharm but my src folder is marked as a source root, and I have it set up so that source roots are added to PYTHONPATH. still nothing – lmotl3 Feb 25 '18 at 15:36

2 Answers2

1

Try from WebScraper.actor import Actor. If this doesn't work its because your package directory is not in the PYTHONPATH. You can set that in the IntelliJ Python run configuration.

The relative import is not working for you because you are trying to run a module as a script. You can see an explanation of what is happening at https://stackoverflow.com/a/8300343/7088038. If you want relative imports to work you will have to add a __main__.py file to your module to allow it to be runnable, or execute from an external script where you use an absolute import so you don't clobber the package namespace.

One other stylistic note- usually (but not always) package names in python use all lowercase names. CamelCase is reserved for class names. So if you wanted to follow convention you would call your package webscraper and use from webscraper.actor import Actor

avigil
  • 2,218
  • 11
  • 18
0

To import a class into your script use:

from actor import Actor

Or to import the .py entirely (including whatever imports included in it) into the namespace use:

from actor import *
manandearth
  • 804
  • 1
  • 9
  • 25
  • if you look through my post, you can see I already tried that to no avail – lmotl3 Feb 25 '18 at 14:36
  • I did see that but I try it and it works. am I missing something? – manandearth Feb 25 '18 at 14:40
  • If I do "from actor import Actor" it tells me actor is an unresolved reference, so that alone isn't the solution – lmotl3 Feb 25 '18 at 14:42
  • I'll clarify the reason it didn't work in my original post – lmotl3 Feb 25 '18 at 14:45
  • might be for using `(Object)` in class definition. New syntax in python3 done away with that. – manandearth Feb 25 '18 at 14:51
  • nope, getting rid of it didn't change the unresolved reference error – lmotl3 Feb 25 '18 at 14:56
  • hmm.. I am missing something, I created a subclass to Actor is a seperate file and I manage to import one to the other and import to a shell without a problem with the simple `from <> import *' could you upload scraper.py, perhaps and I'll try to recreate it on my machine? – manandearth Feb 25 '18 at 15:00