Python: syntax error with import [duplicate]
Asked Answered
T

4

9

I have a python script technically named /home/pi/Adafruit-Raspberry-Pi-Python-Code/Adafruit_BMP085/Adafruit_BMP085_example.py

The first line of this script is

from Adafruit_BMP085 import BMP085

Also located in this directory is a python file named Adafruit_BMP085 that has a function BMP085.

I want to create a python script in /home/pi that imports the same BMP085.

I've tried:

from /home/pi/Adafruit-Raspberry-Pi-Python-Code/Adafruit_BMP085/Adafruit_BMP085 import BMP085

But this just gives me a syntax error:

SyntaxError: invalid syntax

I've tried various syntax combinations of this same method, but cannot find one that works.

Teaser answered 14/3, 2013 at 18:31 Comment(0)
S
15

You need to add the /home/pi/Adafruit-Raspberry-Pi-Python-Code path to the module search path in sys.path:

import sys

sys.path.append('/home/pi/Adafruit-Raspberry-Pi-Python-Code')
from Adafruit_BMP085 import BMP085

or move the Adafruit_BMP085 package to a directory already in your sys.path.

The directory of the script itself is also part of the sys.path, so you could also run:

$ cd /home/pi/Adafruit-Raspberry-Pi-Python-Code
$ cp Adafruit_BMP085/Adafruit_BMP085_example.py .
$ python Adafruit_BMP085_example.py
Stadler answered 14/3, 2013 at 18:32 Comment(0)
M
2

OP's question is generically titled but the post is specific to a particular import case.

In my case, I was getting a SyntaxError when having a very standard import scenario. The error was directly pointing at the import statement, very confusingly.

What was actually happening was that there was a (very subtle) syntax error in the module being imported. Fixing that error resolved the SyntaxError during import.

This was very confusing because Python reported SyntaxError on the import line, rather than forwarding to the internal module's syntax problem (which, I believe, sometimes it does); even a generic ImportError would've been more helpful. I wasted time thinking it was some module/path naming issue.

Malliemallin answered 17/8, 2022 at 11:38 Comment(0)
N
-1

In my case, the problem was a combination of

  1. leaving an open string in a module (the bug...)
  2. importing the module within a try/except block

It happened when using a device with MicroPython, and these were the symptoms:

(1) import mymodule from the REPL prompt didn't show any problem!

(2) running this code from my main module:

try:
    import mymodule
except Exception as e:
    print(e, '|', e.errno, '|', e.value, '|', e.args)

was giving this result at the 'import' line, without revealing the problematic source:

invalid syntax | invalid syntax | invalid syntax | ('invalid syntax',)

(3) but when I did a direct import from the main module, I found the problem:

Traceback (most recent call last):
  File "main.py", line 3, in <module>
  File "mymodule.py", line 126

so... be careful when wrapping import with try/except

Nip answered 16/5, 2023 at 10:47 Comment(0)
S
-2

I had the same problem. The problem first occured when upgrading to Jessie on RPI. The cause was probably within the pathing. Added below line to Python program:

sys.path.append('/home/pi/Adafruit-Raspberry-Pi-Python-Code/Adafruit_BMP085')

Now problem solved.

Selfabsorbed answered 2/3, 2016 at 9:18 Comment(1)
Updating to Jessie should not be affecting what statements python2 or python3 considers syntax errors. It should be operating system independent.Eozoic

© 2022 - 2024 — McMap. All rights reserved.