How to set Environment Variables on Pyrevit extension startup for use in multiple pushbutton scripts
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I want to be able to access certain directories in my extension folder regardless of where the script file is located in my repository. My currents solution is to do something like os.path.join(os.path.dirname(__file__),"bin\icons")) to get to my folder full of png icons.
Some directories however are a bit harder to reach, requiring multiple instances of os.path.dirname(os.path.dirname(os.path.dirname(__file__)))...etc. And its just not the most elegant of solutions IMO.
What I would like to do is call an environment variable such as ENV_VAR_ICONS which would point me to the same directory without having to nest a myriad instances of op.dirname's in each of my individual script files.
I was able to do this with a startup.py script placed in the same directory as my bin, lib, and .tab directories.
#! python3
import os
import os.path as op
import sys
os.environ["DETAIL_EXT_ICONS"] = op.dirname(__file__) + "\\bin\\icons"
To test that this works I have a test .pushbutton which lists my paths, and another which prints out my Cpython environment:
#! python3
# used to manually check PATHS
import os
for k, v in os.environ.items():
print(f'{k}={v}')
#! python3
import sys
print("Python version")
print (sys.version)
print("Version info.")
print (sys.version_info)
print("Paths")
print('\n'.join(sys.path))
But, as you can see, this script is using Cpython, and if I get rid of the #! python3 at the start of the startup.py script, no environment variables are added. To confirm I modified the 2 scripts above to check env variables using python 2 and check the python2 environment:
# -*- coding: utf-8 -*-
# used to manually check PYTHONPATH
import os
PATHS = []
for k, v in os.environ.items():
PATHS.append('{}={}'.format(k, v))
PATHS.sort()
for i in PATHS:
print i
# -*- coding: utf-8 -*-
import sys
print("Python version")
print (sys.version)
print("Version info.")
print (sys.version_info)
print("Paths")
print('\n'.join(sys.path))
I have also tried setting these variables using and __init__.py script placed in the lib folder, and also by copying over pyrevits standard startup.py script file, modifying it to add the variables I would like. Neither of these seemed to work either.
Any other potential solutions to test out would be much appreciated.