SOLUTION POSTED AT BOTTOM
Thanks to the commentators for pointing me in the right direction. Between their help and this other thread I was able to come to a robust solution which is posted at the bottom.
Situation
I have a .py script called job.py
that relies on data in another .py script called data.py
. Both scripts reside in the same folder, 05_scripts
. The 05_scripts
folder sits under Git version control in a parent folder called project
.
Thus, the complete paths to the two scripts are:
project/05_scripts/job.py
and
project/05_scripts/data.py
.
Problem: I cannot import data.py
without renaming the folder 05_scripts
.
In job.py
I want to source the data.py
file. This is problematic because the data.py
file sits in a folder whose name begins with a digit, 05_scripts/
. If I change the folder name to _05_scripts/
or something else so that the first character is non-numeric, then I achieve my goal and import the data.py
script without a problem. However, this is unacceptable since this is part of a much larger project and it is not acceptable to change all references to this folder.
What an acceptable solution must look like
The solution I need is code in job.py
that can successfully import data.py
without renaming the 05_scripts/
folder.
What I have tried
Attempt 1
Input:
from 05_scripts.data import *
Output:
File "<stdin>", line 1
from 05_scripts.data import *
^
SyntaxError: invalid token
Attempt 2
Input:
from '05_scripts'.data import *
Output:
File "<stdin>", line 1
from '05_scripts'.data import *
^
SyntaxError: invalid syntax
Attempt 3
Input:
from '05_scripts.data' import *
Output:
File "<stdin>", line 1
from '05_scripts.data' import *
^
SyntaxError: invalid syntax
Attempt 4
Input:
from data import *
Output:
ModuleNotFoundError: No module named 'data'
Attempt 5
Input:
import data
Output:
ModuleNotFoundError: No module named 'data'
Other attempts
I also tried variations of the solutions posted here. This included different variations of using __import__
and importlib.import_module()
but could not get a working solution that did not include renaming the parent folder.
Recall, this works but is not an acceptable solution for my use case:
After renaming 05_scripts/
to _05_scripts
,
Input:
from _05_scripts.data import *
SOLUTION
# Getting the path for the project and doing string manipulation.
import os
import re
working_dir = re.sub("\\\\", "/", os.getcwd())
scripts_dir = working_dir + "/05_scripts"
# Importing all from the data.py script
import site
site.addsitedir(scripts_dir)
from data import *