Reputation: 427
My project structure looks like this:
Repo/
|-- data/
| |-- a.csv
| |-- b.csv
|
|-- src/
| |-- project/
| |-- c.py
|
|-- .gitignore
|-- README.md
I'm new to structuring my projects like this, as they are mostly private repos, but I thought that it would be nice to start structuring them like "real" public projects to get used to the techniques used. I searched up some recommended python project structures and found this one. I know there are some discussions about whether to use src/
or not, but I just decided to go with this one. (Any suggestions appreciated!)
Now I want to list the files in the data/
directory from my c.py
module. To do so I used os.scandir('data/')
which worked fine for me. But for a friend of mine, who has access to the repo, it didn't. He had to use os.scandir('../../data/')
.
I suspect the reason for this might be that his environment is set up in a different way, so that the /src/project
folder is treated as the working directory, while for me it is the root folder. But again, I don't know much about all of this, so please correct me if I'm wrong.
My question is, what is the right way to access the data/
directory? How would y'all handle it, if it was a public repository?
Any answers/suggestions (also regarding my structure in general) are appreciated. Thank you!
Upvotes: 0
Views: 1659
Reputation: 427
I resolved my problem by defining a constant ROOT_DIR
with the path to my root directory using pathlib
in a new constants
module. I am now using ROOT_DIR
to make the relative paths absolute.
Upvotes: 0
Reputation: 515
As you mentioned; It completely depends on the working directory.
Preventing ../
s might be good practice and to do that; here is my favorite way.
I have a project (a game engine) structured like this (a simplified version):
Root/
|-- build/
| |-- Target1/
| | |-- Target1 (executable)
| |-- Target2/
| |-- Target2 (executable)
|
|-- Target1/
| |-- src/ (all the code)
| |-- assets/ (all the textures, scenes ...)
|
|-- Target2/
| |-- src/ (all the code)
| |-- assets/ (all the textures, scenes ...)
|
|-- .gitignore
|-- README.md
by default, working directory for each Target
is Root/build/<TargetName>
.
but with that, I need a couple of ../
s to reach my assets (load some texture for example). Plus where the final executable is located; is different between a Visual Studio build and a GNU Make build! so this will change the relative path from the executable to my assets on different environments!
Fixing it is simple. set the default working directory for executing any Target
to Root/<TargetName>
.
and both problems are gone! For example, I can use this relative path: assets/wood.png
on all environments.
You can set Root
as the working directory and use this command to run your python script:
python src/project/c.py
and specify paths like this:
data = load_csv("data/a.csv")
Another workaround (like what I did with my game engine) is to put the files like this:
Repo/
|-- project
| |-- data/
| | |-- a.csv
| | |-- b.csv
| |
| |-- src/
| |-- c.py
|
|-- .gitignore
|-- README.md
and set Repo/project
as the working directory.
If you are interested in my little game engine and wanna check out its structure, It lives on GitHub: http://github.com/OverShifted/OverEngine
Upvotes: 1