Python Forum
importing a config file prefixed with a dot
Thread Rating:
  • 3 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
importing a config file prefixed with a dot
#6
Beside dot meaning relative import, import something creates variable something and variable names cant start with a dot.

You can use/abuse importlib library to load module from a file and execute it, make it importable and import it (for python 3.?+, older ones use imp?).

Example (with big help of importlib docs)
Output:
jacq /tmp> cat .boo.py print('boo') def boo():     print('boo boo')
>>> import sys
>>> import importlib.util as imput
>>> spec = imput.spec_from_file_location('boo', '.boo.py')
>>> boo_module = imput.module_from_spec(spec)
>>> spec.loader.exec_module(boo_module)
boo
>>> sys.modules['boo'] = boo_module
>>> import boo
>>> boo.boo()
boo boo
But doing such thing just to have hidden config file seems be a little exaggerated. Is not better to put your config file to some hidden directory (like ~/.config/skapeutil), modify sys.path and import it in a standard way?
Reply


Messages In This Thread
RE: importing a config file prefixed with a dot - by zivoni - Mar-25-2017, 10:14 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  New2Python: Help with Importing/Mapping Image Src to Image Code in File CluelessITguy 0 768 Nov-17-2022, 04:46 PM
Last Post: CluelessITguy
  Problem with importing Python file in Visual Studio Code DXav 7 5,407 Jun-15-2022, 12:54 PM
Last Post: snippsat
  importing functions from a separate python file in a separate directory Scordomaniac 3 1,455 May-17-2022, 07:49 AM
Last Post: Pedroski55
  Updating a config file [solved] ebolisa 8 2,715 Nov-04-2021, 10:20 AM
Last Post: Gribouillis
  Importing a function from another file runs the old lines also dedesssse 6 2,686 Jul-06-2021, 07:04 PM
Last Post: deanhystad
  Is there a library for recursive object creation using config objects johsmi96 0 1,901 May-03-2021, 08:09 PM
Last Post: johsmi96
  help with pytesseract.image_to_string(savedImage, config='--psm 11')iamge to string korenron 0 2,758 Apr-29-2021, 10:08 AM
Last Post: korenron
  Importing text file into excel spreadsheet with formatting david_dsmn 1 3,698 Apr-05-2021, 10:21 PM
Last Post: david_dsmn
  Config file update Olivier74 0 1,526 Aug-18-2020, 03:36 PM
Last Post: Olivier74
  importing a CSV file into Python russoj5 1 3,003 Aug-02-2020, 12:03 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020