Browse the Python Project Reference Directory

Asked 2 years ago, Updated 2 years ago, 29 views

Question

Are there any general settings or conventions in the python project that reference the reference directory?

Background

For example, in Rails, if you use that framework to write code, you can see the variable Rails.root, which is the root directory of the project.For example, Rails.root/'config'/'foo_setting.yml' can resolve the path to Project Route/config/foo_setting.yml

I am currently developing a python project, so I wanted to refer to the contents of the "Project Root/conf/sqlalchemy.yml" file in the code.Each time you specify a relative path in the code that uses that path, you can refer to it, but I think the relative path in the code will be structured as the code base develops and will need to be changed (by moving the code file itself).

So I wanted to resolve the "relative path from the project route" in the code of the application, but how would this be implemented in a typical python project?

python

2022-09-30 21:37

2 Answers

project/
    main.py

importos in main.py and then

root=os.path.dirname(os.path.abspath(__file__))

root contains "path/to/project/main.py".
This is because os.path.abspath gets the absolute path, and __file__ points to your own file path in python's reserved word.

This time it looks like you want a directory name, so

root=os.path.dirname(os.path.abspath(__file__))

I think it would be good to get "path/to/project/" as the .


2022-09-30 21:37

Are there any general settings or conventions in the python project that reference the reference directory?

I think it's off the point of the question, but I found the directory configuration "Recommended?"

I wanted to resolve the "relative path from the project route" in the code of the application, but how would this be implemented in a typical python project?

 "For small projects, ""Keep the directory hierarchy simple"" would be fine."

The configuration files, definitions, and references are as follows:


2022-09-30 21:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.