-2

I tried to import the file 227exercise.py, but keep getting following error:

SyntaxError: Invalid Syntax

atline
  • 28,355
  • 16
  • 77
  • 113

1 Answers1

1

You can do this with importlib.

import importlib
module = importlib.import_module("227exercise")
print(repr(module))
# <module '227exercise' from '/home/ubuntu/227exercise.py'>

However, it is still strongly recommended that you name your python files with valid identifiers (they can't begin with digits).

iBug
  • 35,554
  • 7
  • 89
  • 134
  • @John You can click the grey tick beside this answer to mark it as "accepted". See [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – iBug Jan 05 '19 at 06:40