Reputation: 301
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
.
Upvotes: 1
Views: 8817
Reputation: 12201
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
Upvotes: 1
Reputation: 1476
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.
Upvotes: 1
Reputation: 168967
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:
/home/me/project
myFunctions
, you have a myFunctions/__init__.py
pyproject.toml
to /home/me/project
with the contents listed below.setup.cfg
to /home/me/project
with the contents listed below.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.[build-system]
requires = [
"setuptools>=42",
"wheel"
]
build-backend = "setuptools.build_meta"
[metadata]
name = myFunctions
[options]
packages = find:
Upvotes: 1