简体   繁体   中英

How can you import a local version of a python package?

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?

Try this

import sys

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

import matplotlib # modified

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM