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.
-
Do not mess with PYTHONPATH. It will lead to hard-to-debug situations, non-portable code and general misery.AKX– AKX2022年01月31日 08:45:18 +00:00Commented Jan 31, 2022 at 8:45
-
Ok, thank you, do you have also some other suggestions?ilFonta– ilFonta2022年01月31日 08:47:28 +00:00Commented Jan 31, 2022 at 8:47
3 Answers 3
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:
- 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 amyFunctions/__init__.py - you know how to use virtualenvs for your other projects
- you have a project directory, e.g.
- Add
pyproject.tomlto/home/me/projectwith the contents listed below. - Add
setup.cfgto/home/me/projectwith the contents listed below. - Now, in a project virtualenv where you want to use
myFunctions, runpip 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:
2 Comments
csv.py in one of your own directories, you suddenly can't use the standard library csv module.)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
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
site-packages?