Skip to main content
Make the code work and use removesuffix.
Source Link
Bakuriu
  • 102.6k
  • 23
  • 209
  • 236

Python does not automatically import submodules contained in a package. Hence import tracks only loads tracks/__init__.py.

However you can put code inside the __init__.py file that imports all the modules it finds in that directory.

For example putting something like this in the __init__.py:

import os import importlib __globals = globals() for file in os.listdir(os.path.dirname(__file__)): mod_name = file[:-3] file.removesuffix(".py")  # strip .pyif atmod_name thein end ("__init__", "__pycache__"): continue  __globals[mod_name] = importlib.import_module('.' + mod_name, package=__name__) 

Should make your submodules available as tracks.trackX when importing only tracks.

Or you could use exec:

import os import importlib for file in os.listdir(os.path.dirname(__file__)): mod_name = file[:-3]file.removesuffix(".py")  # stripif .pymod_name atin the("__init__", end"__pycache__"): continue exec('import .' + mod_name) 

A cleaner approach would be to use import hooks or implement your own custom module importer. There are multiple ways to do this using importlib see also sys.path_hooks

Python does not automatically import submodules contained in a package. Hence import tracks only loads tracks/__init__.py.

However you can put code inside the __init__.py file that imports all the modules it finds in that directory.

For example putting something like this in the __init__.py:

import os import importlib __globals = globals() for file in os.listdir(os.path.dirname(__file__)): mod_name = file[:-3]  # strip .py at the end  __globals[mod_name] = importlib.import_module('.' + mod_name, package=__name__) 

Should make your submodules available as tracks.trackX when importing only tracks.

Or you could use exec:

import os import importlib for file in os.listdir(os.path.dirname(__file__)): mod_name = file[:-3] # strip .py at the end exec('import .' + mod_name) 

A cleaner approach would be to use import hooks or implement your own custom module importer. There are multiple ways to do this using importlib see also sys.path_hooks

Python does not automatically import submodules contained in a package. Hence import tracks only loads tracks/__init__.py.

However you can put code inside the __init__.py file that imports all the modules it finds in that directory.

For example putting something like this in the __init__.py:

import os import importlib for file in os.listdir(os.path.dirname(__file__)): mod_name = file.removesuffix(".py")  if mod_name in ("__init__", "__pycache__"): continue  importlib.import_module('.' + mod_name, package=__name__) 

Should make your submodules available as tracks.trackX when importing only tracks.

Or you could use exec:

import os import importlib for file in os.listdir(os.path.dirname(__file__)): mod_name = file.removesuffix(".py")  if mod_name in ("__init__", "__pycache__"): continue exec('import .' + mod_name) 

A cleaner approach would be to use import hooks or implement your own custom module importer. There are multiple ways to do this using importlib see also sys.path_hooks

Python does not automatically import submodules contained in a package. Hence import tracks only loads tracks/__init__.py.

However you can put code inside the __init__.py file that imports all the modules it finds in that directory.

For example putting something like this in the __init__.py:

import os import importlib __globals = globals() for file in os.listdir(os.path.dirname(__file__)): mod_name = file[:-3] # strip .py at the end __globals[mod_name] = importlib.import_module('.' + mod_name, package=__name__) 

Should make your submodules available as tracks.trackX when importing only tracks.

Or you could use exec:

import os import importlib for file in os.listdir(os.path.dirname(__file__)): mod_name = file[:-3] # strip .py at the end exec('import .' + mod_name) 

A cleaner approach would be to use import hooks or implement your own custom module importer. There are multiple ways to do this using importlib see also sys.path_hooks

Python does not automatically import submodules contained in a package. Hence import tracks only loads tracks/__init__.py.

However you can put code inside the __init__.py file that imports all the modules it finds in that directory.

For example putting something like this in the __init__.py:

import os import importlib __globals = globals() for file in os.listdir(os.dirname(__file__)): mod_name = file[:-3] # strip .py at the end __globals[mod_name] = importlib.import_module('.' + mod_name, package=__name__) 

Should make your submodules available as tracks.trackX when importing only tracks.

Or you could use exec:

import os import importlib for file in os.listdir(os.dirname(__file__)): mod_name = file[:-3] # strip .py at the end exec('import .' + mod_name) 

A cleaner approach would be to use import hooks or implement your own custom module importer. There are multiple ways to do this using importlib see also sys.path_hooks

Python does not automatically import submodules contained in a package. Hence import tracks only loads tracks/__init__.py.

However you can put code inside the __init__.py file that imports all the modules it finds in that directory.

For example putting something like this in the __init__.py:

import os import importlib __globals = globals() for file in os.listdir(os.path.dirname(__file__)): mod_name = file[:-3] # strip .py at the end __globals[mod_name] = importlib.import_module('.' + mod_name, package=__name__) 

Should make your submodules available as tracks.trackX when importing only tracks.

Or you could use exec:

import os import importlib for file in os.listdir(os.path.dirname(__file__)): mod_name = file[:-3] # strip .py at the end exec('import .' + mod_name) 

A cleaner approach would be to use import hooks or implement your own custom module importer. There are multiple ways to do this using importlib see also sys.path_hooks

Source Link
Bakuriu
  • 102.6k
  • 23
  • 209
  • 236

Python does not automatically import submodules contained in a package. Hence import tracks only loads tracks/__init__.py.

However you can put code inside the __init__.py file that imports all the modules it finds in that directory.

For example putting something like this in the __init__.py:

import os import importlib __globals = globals() for file in os.listdir(os.dirname(__file__)): mod_name = file[:-3] # strip .py at the end __globals[mod_name] = importlib.import_module('.' + mod_name, package=__name__) 

Should make your submodules available as tracks.trackX when importing only tracks.

Or you could use exec:

import os import importlib for file in os.listdir(os.dirname(__file__)): mod_name = file[:-3] # strip .py at the end exec('import .' + mod_name) 

A cleaner approach would be to use import hooks or implement your own custom module importer. There are multiple ways to do this using importlib see also sys.path_hooks