As you develop more and more Python code for data science tasks, you may find yourself using the same code (functions) over and over again (e.g., a customized eda function). Instead of copying and pasting code snippets to your current Jupyter notebook, there are slicker ways to call Python functions from anywhere on your computer without having to specify the path, which I’ll describe in this blog.
Note: Methods described here are applicable to UNIX systems (Mac or Linux).
In the terminal, go to your home directory (
cd and press “enter”).
ls -a to see all the files in your home directory, including hidden files. There should be a file named
Open it with an text editor of your choosing. At the end of the document, add the following line:
export PYTHONPATH="<path to directory containing your Python script>:$PYTHONPATH"
For example, mine is :
:$PYTHONPATH at the end makes sure that you append to
PYTHONPATH, and not overwrite it.
For this to take effect, run
source ~/.bashrc or simply log out and then log in again.
.bashrc is read at log-in, that’s why you need to
source it if you don’t want to log in again.
Now, to use the function(s) in your
.py file(s), simply run
import <script> in your Jupyter notebook.
For example, I have my functions saved in
eda.py, all I need to do is to add
import eda or
from eda import * to my Jupyter notebook.
If you are ready to package your script(s) and share it with the world, you can register it with PyPI, which you (or anyone else) can
pip install in the system (or a virtual environment), and then
import it like you would any other Python libraries. You can find more details at the resources below: