Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to add my project directory to the system path in every script to import a function from another directory?

Tags:

python

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.')
like image 415
blahblahblah Avatar asked Feb 13 '18 04:02

blahblahblah


People also ask

How do I add a new directory to my $Path variable?

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 path?

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.

How do I run a shell script from a specific directory?

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 ...

How do I check what directories are in my $path?

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.


1 Answers

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. ✌


A module is a single .py file (or files) that are imported under one import and used. βœ”

import aModuleName # Here 'aModuleName' is just a regular .py file.

Whereas, a package is a collection of modules in directories that give a package hierarchy. A package contains a distinct __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

I hope this simple explanation clarifies your doubts on Python imports' mechanism and solves the problem. If not then do comment here. 😊

like image 118
Sushant Chaudhary Avatar answered Oct 23 '22 04:10

Sushant Chaudhary