I have a github actions workflow where I run many different files, for which I append to the PYTHONPATH all the paths I need for the scripts to work. To do so, I created another program called workflow_import.py in the same directory as my main scripts where I simply add a function of this sort:
def load_modules():
if 'SOURCES_PATH' in os.environ:
print('SOURCES_PATH') # debug line
sources_path_list = eval(os.environ['SOURCES_PATH'])
sys.path.extend(sources_path_list)
I add this function to each of my scripts as in
from workflow_import import load_modules
Herein, I work with a environment variable called 'SOURCES_PATH' (aka. variables.SOURCES_PATH, don't know if saving it as a secret is more convenient) storing all the modules' location. Running the main script doesn't invoke any ModuleNotFindError. However, one of my scripts generates a new subfolder called \ref, and since it most certainly changes to that directory all of the subsequent scripts fail to find workflow_import.py or any other module from load_modules().
I thought this was due to the sys.path() changing once \ref is created, but I don't know how to make it so it stays consistent within the workflow. As an example, I run the scripts in this manner:
- name: Library Test
shell: powershell
run: |
& $env:PYTHON_ENV_PATH ./libraries/library/tests/generate_reffiles.py
Where PYTHON_ENV_PATH is another secret containing the python.exe path of my desired environment.
Edit:
An example of SOURCES_PATH:
[
'C:\\actions-runner\\_work\\repo\\module1',
'C:\\actions-runner\\_work\\repo\\module2',
...
]
Update
I think I managed to solve the issue. In my case, as I was working with a conda-environment I had to activate first the environment and then update the PYTHONPATH with:
"PYTHONPATH=${env:SOURCES_PATH}" >> $env:RUNNER_ENV
However, as I run a cmd routine containing a python script each time, there is another solution consisting in prepending sys.path.extend(sources_path_list), in a way like PyCharm does it.
SyntaxErrorSOURCES_PATHlooks like a python list such as['./foo','./bar']whicheval()will process correctly.. it's a huge security risk though as anyone who can modify that env variable can make the script do whatever they want.SOURCES_PATH's content. Is it risky if the env var can only be modified by repo's admins? Anyway, I presume these paths are absolute within the C: drive, so that's why I don't get the nature of the issue. Ussing sys.expand() on a local python runner doesn't present any errors.