ImportError: "unknown location"

Hi all,

So, it looks like I’ve taken a few steps backward. I’m trying to re-familiarize myself with my lexicon.py and parser.py modules, but I can’t import the any of my functions from parser.py. After importing the module, I’ll try to import the peek functions and get this error message in the interpreter:

ImportError: cannot import name 'peek' from 'parser' (unknown location)

I’ve even gone in and used the built in globals() function call to make sure it’s in my global namespace:

'peek': <function peek at 0x0000016896F1C1E0>

Sure enough, it’s there. In fact, the only object I’m able to import from the module is the ParserError exception. I’ve made sure there aren’t any improper indentations, misspellings, etc. I also can’t find anything online to help me out with an object having an “unknown location.”

For reference, here’s my directory tree:

gorthonweb
├───bin
├───docs
├───gorthonweb
│   └───templates
└───tests

Everything except for my app.py module is in the gorthonweb subdirectory. I’m also not sure if this would cause anything, but I’ve actually been storing code in my __init__.py module too. It looks like this:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager

app = Flask(__name__)
app.config['SECRET_KEY'] = 'random numbers'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///game.db'
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)
login_manager = LoginManager(app)
login_manager.login_view='login'

from gorthonweb import routes

P.S. I’ve also tried setting my PYTHONPATH and I am aware I misspelled gothon (gorthon), but I’ve been spelling it that way everywhere.

So templates is in gorthonweb also?
If it’s a subdirectory of gorthonweb, check if it has an __init__.py file in it. If it does and you don’t have an __init__.py in the directory of your parser module, you’ll get an error.
So the solution would be to add an __init__.py or delete the one in the templates. You eiter have them everywhere or not.
Check this out: https://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html

That’s what each directory looks like. I only have an __init__.py file in the gorthonweb directory (where my parser.py module is). Also, I’m able to import everything from my lexicon.py module just fine (which is why I’m very confused). I don’t have one in the root directory, where my app.py module is.

Show me where you do the import of the parser module, exactly how you import “peek”

I also forgot to mention the problem isn’t just peek, it’s with everything except for the ParserError class.

Right now I’m doing it in the python interpreter. I also went ahead and tried running it through a script:

# scratch.py
from lexicon import lexicon, scan
from parser import *

words = 'the player goes north'
scanned_words = scan(words)
parsed_words = parse_sentence(scanned_words)

print(parsed_words)

It returns this error message when I try to run the script:

Traceback (most recent call last):
  File "scratch.py", line 6, in <module>
    parsed_words = parse_sentence(scanned_words)
NameError: name 'parse_sentence' is not defined

Why don’t you do:
import parser as pars
then do: parsed_words = pars.parse_sentence(scanned_words)

Still returns the same message. :frowning:

Wait… Where is scratch.py located?
Ahhh! I think the name of the module is the problem! Python already has a module parser for sure. Try renaming it.
There must be an issue with script location or Python path in Windows.

3 Likes

It’s located in the same directory as all of the other ones. I just created it temporarily.

And I think that did it! Wow! I guess there’s a naming confliction then with something they included in 3.7. I changed it to analyzer.py and it worked.

Now I have to re-familiarize myself with this. But, at least I can access the objects finally. :slight_smile:

Thanks, @io_io!

2 Likes

No problem, glad you solved it! :smiley:

1 Like

Yes! That’s exactly what I was about to say. I totally forgot about that. Python is terrible for not putting builtins into a module.

I think I need to go read up on all the changes made in 3.7. This is now the second problem I’ve had due to the update.

I am still using 3.6 and not planning to change soon. Didn’t even install 3.7

1 Like

Don’t blame you there.

It’s very interesting.

Yeah it’s good practice to read up in release notes before an upgrade, especially a dot upgrade like .6 to .7

I get along find with 3.7 actually.

hello lm facing a challenge with import error unkown location too
"ImportError: cannot import name ‘GazeTracking’ from ‘gaze_tracking’ (unknown location)"is the messagel get when l run app.py tho gaze tracking is in the same file as ap.py


thats my snippet for app.py can you help

A free service run by Zed A. Shaw for learncodethehardway.org.