First time here? Check out the FAQ!
1

why does "import filename" give a SYNTAX error

  • retag add tags

I have a file A.py in the project directory and another file B.py in the same directory. In B I have the statement "import A" in the first line. I get the following error message:

Syntax Error: invalid syntax:

The same thing happens with : import A.py, import 'A.py', etc. The error is not that the file is missing or that the file has errors, but rather the import statement is syntactically incorrect.

What to do???

asked 2022-07-21 10:26:34 -0600
This post is a wiki. Anyone with karma >75 is welcome to improve it.
edit flag offensive 0 remove flag close merge delete

Comments

add a comment see more comments

1 Answer

0

For the record, this was resolved by email. The problem was that the imported filename was not A.py but rather a name that contained dashes like A-1.py. The syntax error was due to 'import A-1' not being valid Python. Python files can be named using dashes in the name but then can only be run directly and not imported. Using underscores as in A_1.py solves the problem since 'import A_1' is valid syntax.

Wingware Support's avatar
4.2k
Wingware Support
answered 2022-07-22 08:21:50 -0600
edit flag offensive 0 remove flag delete link

Comments

add a comment see more comments

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss.

Add Answer