I'm trying to keep a data science project well-organized so I've created a directory inside my src
directory called utils
that contains a file called helpers.py
, which contains some helper functions that will be used in many scripts. What is the best practice for how I should import func_name
from src/utils/helpers.py
into a file in a totally different directory, such as src/processing/clean_data.py
?
I see answers to this question, and I've implemented a solution that works, but this feels ugly:
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))))
Am I doing this right? Do I need to add this to every script that wants to import func_name
, like train_model.py
?
My current project folder structure:
myproject
/notebooks
notebook.ipynb
/src
/processing
clean_data.py
/utils
helpers.py
/models
train_model.py
__init__.py
Example files:
# clean_data.py
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))))
from src.utils.helpers import func_name
func_name()
# helpers.py
def func_name():
print('I'm a helper function.')
Open the file with your text editor and add the following line at the end of it: Save the file and load the new $PATH into the current shell session using the source command: To confirm that the directory was successfully added, print the value of your $PATH by typing: Adding new directories to your user or global $PATH variable is pretty simple.
Is it OK to add the .NET framework directories to my system PATH? If so, which ones should I add and in which order? Show activity on this post. Sure, nothing wrong with that. First thing I did once I got tired of digging out installutil and/or gacutil. You should only need to add: ...depending on whether or not you want 64bit utilities or not.
Letβs say you have a directory called bin located in your Home directory in which you keep your shell scripts. To add the directory to your $PATH type in: export PATH="$HOME/bin:$PATH". The export command will export the modified variable to the shell child process environments. You can now run your scripts by typing the executable script name ...
To check what directories are in your $PATH, you can use either the printenv or echo command: The output will look something like this: If you have two executable files sharing the same name located in two different directories, the shell will run the file that is in the directory that comes first in the $PATH.
First of all, let me describe you the differences between a Python module & a Python package so that both of us are on the same page. β
import aModuleName
# Here 'aModuleName' is just a regular .py file.
__init__.py
file. βfrom aPackageName import aModuleName
# Here 'aPackageName` is a folder with a `__init__.py` file
# and 'aModuleName', which is just a regular .py file.
Therefore, when we have a project directory named proj-dir of the following structure ‡
proj-dir
--|--__init__.py
--package1
--|--__init__.py
--|--module1.py
--package2
--|--__init__.py
--|--module2.py
π Notice that I've also added an empty
__init__.py
into the proj-dir itself which makes it a package too.
π Now, if you want to import any python object from module2 of package2 into module1 of package1, then the import statement in the file module1.py would be
from package2.module2 import object2
# if you were to import the entire module2 then,
from package2 import module2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With