https://stackoverflow.com/questions/25389095/python-get-path-of-root-project-structure
You can do this how Django does it: define a variable to the Project Root from a file that is in the top-level of the project. For example, if this is what your project structure looks like:
project/
configuration.conf
definitions.py
main.py
utils.py
In definitions.py
you can define (this requires import os
):
ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) # This is your Project Root
Thus, with the Project Root known, you can create a variable that points to the location of the configuration (this can be defined anywhere, but a logical place would be to put it in a location where constants are defined - e.g. definitions.py
):
CONFIG_PATH = os.path.join(ROOT_DIR, 'configuration.conf') # requires `import os`
Then, you can easily access the constant (in any of the other files) with the import statement (e.g. in utils.py
): from definitions import CONFIG_PATH
.
'C Lang > Python Program Diary' 카테고리의 다른 글
라이브러리 읽기 대상 디렉토리를 추가하기 : sys.path.append, os.path.dirname (0) | 2019.07.26 |
---|---|
python에서 페키지 만들어서 test.pypi에 배포 후 import해서 사용해보기 (0) | 2019.07.05 |
Djangot 듀토리얼#1 : Django에 대한 사전지식 (0) | 2019.06.28 |
파이썬 Exception hierarchy (0) | 2019.06.27 |
파이썬 에러처리 베스트프렉티스 (0) | 2019.06.27 |