1

I'd like to collect all the functions I wrote in Python and store them in a folder. I'm an Ubuntu user, what environmental path do I have to add in my ~/.profile?

I tried

export PATH:$PATH:/home/functionFolder

or

export PYTHONPATH:/home/functionFolder

I also added an init.py file in the /home/functionFolder, but it doesn't work.

My aim is to import the functions with

from myFunctions import function1

with myFunctions located in /home/functionFolder.

2
  • Do not mess with PYTHONPATH. It will lead to hard-to-debug situations, non-portable code and general misery. Commented Jan 31, 2022 at 8:45
  • Ok, thank you, do you have also some other suggestions? Commented Jan 31, 2022 at 8:47

3 Answers 3

1

Do not mess with PYTHONPATH. It will lead to hard-to-debug situations, non-portable code and general misery. (If you really, really want to, you could mess with it within your program with e.g. sys.path.insert(0, '...'), but that's also non-portable.)

If you want to maintain a personal toolbox library, it's better to just make it a package you can install. This will also pave the way to making it distributable later on should you want to.

The canonical guide to packaging Python libraries lives here at packaging.python.org but the very simplest tl;dr edition (at the time of writing) is:

  1. This assumes
    • you have a project directory, e.g. /home/me/project
    • there is a package directory in the project directory, e.g. if your package is named myFunctions, you have a myFunctions/__init__.py
    • you know how to use virtualenvs for your other projects
  2. Add pyproject.toml to /home/me/project with the contents listed below.
  3. Add setup.cfg to /home/me/project with the contents listed below.
  4. Now, in a project virtualenv where you want to use myFunctions, run pip install -e /home/me/project. This will install the package that lives in that path in editable mode, i.e. just link that folder into the project's environment.

pyproject.toml

[build-system]
requires = [
    "setuptools>=42",
    "wheel"
]
build-backend = "setuptools.build_meta"

setup.cfg

[metadata]
name = myFunctions

[options]
packages = find:
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you again. It's very tricky! I am mainly a R programmer, it is very easy to address the function path, just save it in a directory and specify it when you use it, like source("/home/Script/R/Funzioni/deciBel.r"). Is it possible to do the same with python?
It's possible but not recommended. Anyone else trying to use that code would need to have the exact same filesystem layout as you, and debugging things gets harder (e.g. if you accidentally have, saaay, csv.py in one of your own directories, you suddenly can't use the standard library csv module.)
1

You can do it by adding the following commands to your .bashrc file which is located in ~/:

PYTHONPATH=/home/functionFolder/:$PYTHONPATH
export PYTHONPATH

once you have added the above commands in the .bashrc file, save it and type following in your console:

source ~/.bashrc

afterward, you can access and import your functions anywhere.

Comments

1

Python makes uses of a specific user-related directory for storing packages and modules that are installed by and for that user only (that is, not system-wide). The place for modules and packages is

$HOME/.local/lib/pythonx.y/site-packages/<module-or-package>

Where x.y denotes the relevant Python version, e.g. 3.9, and <module-or-package> is the .py file for a module, and a directory for the package. (Note that this means you can use multiple minor Python versions next to each other without them interfering, but you do have install packages for each minor Python version separately.)

This directory is automatically picked up by Python when the relevant user uses Python; no need to involve PYTHONPATH.


pip also installs into this directory, when you specify the --user flag; or when pip finds it can't install in the system directory and it will use the $HOME/.local directory instead.

If you have installable packages, you can even use pip install . --user or similar to install the packages properly in $HOME/.local

3 Comments

This doesn't answer OP's question.
@AKX: it doesn't, but it provides an alternative that is possibly better. If people don't know about .local, but do know about PYTHONPATH, you may end up with an a-b question. I can't tell, but I offer the suggestion anyway. The OP can then decide for themselves whether they find this information useful, and feel it is actually a better solution, than setting PYTHONPATH. Or they go for another answer that does tell how to update PYTHONPATH more permanently.
Um... OP says they have a bunch of their own code, not a package – from this answer I'd infer you're recommending they copy their own code into site-packages?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.