1

Following this anwer, how do you do when your application folder start with a decimal (for instance : 00_build, 01_run, 02_parseResult)?

I get the following error: From 01_run.foo import bar

SyntaxError: invalid decimal literal

I tried this From '01_run'.foo import bar

SyntaxError: SyntaxError: invalid syntax

Guillaume D
  • 2,202
  • 2
  • 10
  • 37
  • Python identifiers, including modules, cannot start with a digit. You can play with `__import__` and other stuff, but it's bad practice anyway – Marat Jul 29 '20 at 13:57

1 Answers1

1

The importlib may be what you need:

import importlib
foo = importlib.import_module('01_run.foo')
bar = foo.bar

The reason why you can get bar in this way is that:

The most important difference between these two functions is that import_module() returns the specified package or module (e.g. pkg.mod), ...

It also works in the Py2 environment, provided that the 01_run is a Py2 module. You may need to add a __init__.py under the folder 01_run if it doesn't exist before.

Read the official page of importlib for more details if you use Py3, read this instead if you use Py2.

fengdasu
  • 26
  • 4
  • that is what i though but i have issue then importing globals from it... anyway i think this is the solution, ty – Guillaume D Jul 29 '20 at 14:32