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

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

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.

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

Related

how to use scikit-learn-intelex?

Recently whenever I installed a new enviroment, I got below message a lot. Question is after installing this, How do I use this new package? For my existing file, do I need to change all relevant import packages statements?
Windows 64-bit packages of scikit-learn can be accelerated using scikit-learn-intelex.
More details are available here: https://intel.github.io/scikit-learn-intelex
For example:
$ conda install scikit-learn-intelex
$ python -m sklearnex my_application.py
I found a webpage about this:
https://pypi.org/project/scikit-learn-intelex/
look like everything is the same( like import functions, packages) except adding from sklearnex import patch_sklearn? Is it correct?
Correct,basically the way this would work - for some of sklern calls optimized version would be used instead of stock scikit-learn.
There are also other ways how you can patch https://intel.github.io/scikit-learn-intelex/what-is-patching.html#term-patching
For example you can do global patching that would not require changes in python scripts/notebooks
python sklearnex.glob patch_sklearn

How to import a module from PyPI when I have another module with the same name

I'm trying to use the lockfile module from PyPI. I do my development within Spyder. After installing the module from PyPI, I can't import it by doing import lockfile. I end up importing anaconda/lib/python2.7/site-packages/spyderlib/utils/external/lockfile.py instead. Spyder seems to want to have the spyderlib/utils/external directory at the beginning of sys.path, or at least none of the polite ways I can find to add my other paths get me in front of spyderlib/utils/external.
I'm using python2.7 but with from __future__ import absolute_import.
Here's what I've already tried:
Writing code that modifies sys.path before running import lockfile. This works, but it can't be the correct way of doing things.
Circumventing the normal mechanics of importing in Python using the imp module (I haven't gotten this to work yet, but I'm guessing it could be made to work)
Installing the package with something like pip install --install-option="--prefix=modules_with_name_collisions" package_name. I haven't gotten this to work yet either, but I'm guess it could be made to work. It looks like this option is intended to create an entirely separate lib tree, which is more than I need. Source
Using pip install --target=lockfile_from_pip. The files show up in the directory where I tell them to go, but import doesn't find them. And in fact pip uninstall can't find them either. I get Cannot uninstall requirement lockfile-from-pip, not installed and I guess I will just delete the directories and hope that's clean. Source
So what's the preferred way for me to get access to the PyPI lockfile module?

How to import a module with a dotted path?

I want to import the paramiko module located in /usr/local/lib/python2.7/dist-packages. So, I imported it this way:
from usr.local.lib.python2.7.dist-packages import paramiko
I have an error syntax related to python2.7 (It considers 7 as a package located in python2 package)
I have both Python3.1.3 and Python2.7 installed. I program with Python3.1.3 only, however.
How can I resolve this problem ?
How about ?
import sys
sys.path.append('/usr/local/lib/python2.7/dist-packages')
import paramiko
UPDATED
The best solution is installing paramiko on Python3 env. Take a look at #DanielRoseman's answer. Or virtualenv is worth consideration. Here is a good tutorial. http://simononsoftware.com/virtualenv-tutorial/
I don't know why you think you need to include the full path. That directory will already be included in the Python path. You just need to do import paramiko.
Edit after comment Well you can't randomly import things that are installed for a different version. There are several backwards incompatibilities, and anything that has any compiled extensions will just not work at all.
You need to download and install paramiko for your 3.1 installation, rather than trying to use the 2.7 version. python3 pip install paramiko, as an example.
(Also, you shouldn't really be using 3.1. If you're using the Python 3 series you should upgrade to 3.4.)

Trouble in installing Basemap in matplotlib

I am trying to install Basemap, and beforehand I already have the prerequisite versions of Matplotlib, Python and Numpy working on my Mac. To install Basemap I am following the instructions in this website http://matplotlib.org/basemap/users/installing.html
I seemed to progress quite well with the steps, until the very end which is "Check your installation by running from mpl_toolkits.basemap import Basemap at the python prompt." I got "ImportError: cannot import name pyproj" from a line that reads "---> 30 from mpl_toolkits.basemap import pyproj".
Does anyone have any idea? Many thanks!
The compiled module could be accessible by python. To achieve that you should put the module into python path or you need to add the location of the module to PYTHONPATH environment variable.
To see the python installations paths you can write these lines to python shell:
import sys
for path in sys.path:
print(path)
The code will show the paths python is looking for modules.
After you compile the c library, you need to go to upper folder and run python setup.py install as it's said in the installation page. This will put python modules into one of your python paths.
I had the same problem installing basemap-1.0.7.
I found that the file
/usr/lib64/python2.6/site-packages/mpl_toolkits/basemap/init.py
had a reference to axes-grid1, but python lists only module axes_grid.
So I changed grid1 to grid and now basemap imports without error.
Had the same issue on OSX, found after much faffing that Basemap worked fine with a non-native version of python (for me 2.7.12 with everything under /opt/local/Library/Frameworks...) but not with the native version (for me 2.7.10 with most stuff under /System/Library/Frameworks... and Basemap under /Library/...). I did note also that under the native version there was already some mpl_tooklit content and without the permission to add Basemap there you end up with mpl_toolkit contents in multiple places. I wasn't sure if this was the problem specifically but as I said having it all under opt using non-native python was what solved this for me.

Python 2.4 Doesn't Load Home-Directory Packages

I'm trying to get a Python package to install to my home directory because I don't have the privileges to install it system-wide.
The package is PyProj, and I am trying to install it using python setup.py install --home=~ (with Python 2.4.3), as recommended in the Python documentation. The package compiles successfully and copies itself to what I assume are the correct directories (the directory ~/lib64/python/pyproj appears during install).
But, when I load Python up and type import pyproj, I'm told ImportError: No module named pyproj.
Any thoughts on what might be going on?
You'll need to set PYTHONPATH to tell Python where to locate your locally installed packages.
For example:
[you#home]$ export PYTHONPATH="~/lib64/python"
Or, to do this within the interpreter (or script):
import sys, os
sys.path.append(os.path.expanduser("~/lib64/python"))
For more information on how Python locates installed modules, see section on The Module search Path in docs.
~/lib64/python/pyproj is not part of your PYTHONPATH. There are two or three ways around this, depending on your needs.
The first is to directly modify the path in your module, suitable if you're only going to use it from one module. As noted in the comments, this method does not do expansion on the '~' character.
import sys
sys.path.append('/home/username/lib64/python')
import pyproj
The second way is to add ~/lib64/python/pyproj to your system's PYTHONPATH, through whatever method your system suggests. A line in .bash_profile is shown below.
export PYTHONPATH=$PYTHONPATH:~/lib64/python/pyproj
See the Python Documentation for more details.

Categories