How can you import a local version of a python package?
Asked Answered
H

3

6

I think I've found a bug in matplotlib. I'm using anaconda as a package manager, but had to download matplotlib from github in order to edit it.

How do I import my modified version of matplotlib in order to test it? I tried using

import /absolute/path/to/modified/matplotlib

, but that didn't work. Ideally I would like to create a conda environment that uses the modified matplotlib instead of the original, so I can easily switch between the two.

How do you test and run a modified version of an open source library, without messing up your original version of the package? Is there a way to import a library from an absolute path?

Heavyfooted answered 14/12, 2017 at 17:52 Comment(1)
You can possibly build a conda package (matplotlib.org/users/installing.html#conda-packages) of your modified source and install it into a new environment, but you don't say which OS you're on to know how difficult this will be. On Windows, this will be rather difficult. On *nix, somewhat easierLarose
S
1

Try this

import sys

sys.path.append('/absolute/path/to/modified/matplotlib')

import matplotlib # modified
Simonson answered 14/12, 2017 at 17:56 Comment(5)
Ah yes, this temporarily changes your python path which is useful for what I need (I don't want permanent changes). However, because the library is appended, the original library takes precedence and this doesn't work. I've simply edited it to the following though, which works: sys.path.insert(1, '/absolute/path/to/modified/matplotlib')Heavyfooted
I'll leave this open for some more time, should anyone also find a solution with conda (or virtualenv) environments. I think environments could provide a more elegant solution and it could be useful for other people. If no environment solutions come up, I'll make this the accepted answer. Thank you!Heavyfooted
Although the matplotlib import showed the new version, I've noticed that "import matplotlib.pyplot as plt" raises an "ImportError: cannot import name '_path'". Not sure if this is something that went wrong during matplotlib installation, or if this is due to how matplotlib was imported...Heavyfooted
If you put the modified library package in the working folder (or a link to it in the working folder) the import will take precedence over the system installed libs.Hortensiahorter
PyCharm does not automatically consider the change of the system path regarding code navigation. The behavior can be influenced through the project structure settigns. Also see #77022104Suffix
L
1

Another option not mentioned, if you just put the matplotlib module (copy or move) in the directory of your project, python will check there first, find the version you put there, and look no further. This is exactly the reason why you shouldn't name your files, for example, math.py.

Leveret answered 14/12, 2017 at 23:57 Comment(0)
T
0

You can install a local version by telling anaconda to install the tar-ball of the package directly, i.e.

conda install package-version-py27.tar.bz2

You might also be able to use the --use-local argument.

See: https://github.com/conda/conda/issues/5266, https://github.com/conda/conda/issues/1884

Tebet answered 14/12, 2017 at 23:48 Comment(1)
This would only help if they already had a tarball of the conda package. Instead, they have the source directory...Larose

© 2022 - 2024 — McMap. All rights reserved.