where packages are stored in python (trying to install dragnet) - python-2.7

I've been struggling to install a python package called dragnet and its dawned on me just how little I understand about where packages are stored and how they are accessed in python. When I do import X in python, what steps does python take to try to import the packages?
I have anaconda installed, and it looks like many are stored in ~/anaconda/pkgs with names like "argcomplete-0.8.4-py27_0".
A bunch of other packages are in /Library/Python/2.7/site-packages. Sometimes conda install X doesnt work, but pip install will (conda pip is depreciated), and in these cases packages will often end up here. Both planout and dragnet are examples of such packages.
Dragnet, however, didn't want to import even despite its presence in /Library/Python/2.7/site-packages. When I go directly into the folder /Library/Python/2.7/site-packages in ipython it will import ok. I could not figure out where to put it or how to make it accessible when not in the folder though? I'd appreciate a pointer to a good tutorial or overview about packaging, package locations and importing in python.

In cases in which you are unable to install with anaconda, and you need to install another way, and that other way ends up with the install being not in the anaconda packages folder (lets say you end up with a new install in /Library/Python/2.7/site-packages), a reasonable work around is the following:
import sys
sys.path.append('/Library/Python/2.7/site-packages')
import desired_package_found_in_that_folder
This will force python to look in that folder for packages.

Instead of installing packages globally use Virtualenv to create virtual environments. This will help you isolate different projects and will also make dependency and package management much easier.
Read the docs here: https://virtualenv.pypa.io/en/latest/

Related

Anaconda environments packages update

Let's assume I perform a full anaconda installation as root that will be shared among all users.
a. On the base environment I install python2.7.12, matplotlib and numpy:
$ conda install python=2.7.12 matplotlib numpy
b. After a couple of months one of my users creates an environment:
$ conda install -n py27 python=2.7.12 matplotlib numpy
Let's assume that in the meanwhile a matplotlib and numpy were updated and are no longer on the same version.
My questions are:
will the versions of matplotlib and numpy installed in a) be automatically updated to the latest?
when the environment is created in b), which versions of matplotlib and numpy are installed? the latest ones or the ones that were installed on the base environment in a)?
The reason I am asking this is because I attempting to implement a shared anaconda environment while minimizing the disk footprint. Basically, the reason is to avoid having for each user a different installation of anaconda and avoid having 20 different versions of matplotlib and numpy (among many others) taking up disk space.
Thanks.
a.) The versions of installed packages will not be updated automatically , packages are only updated when you explicitly issue the update commands with your package manager
b.) When a new environment is created and you try to install packages again , by default it will install the latest package (which may not match with whatever is present on your base environment)
Edit - I would like to comment that using a requirements.txt file is a much better way to ensure everyone uses the same versions of the installed libraries.
or else you can make sure everyone mentions the version numbers along with the install command
eg : pip install 'package_name==version_number'

New point of view: pip dealing with multiple Python versions, Canopy, Anaconda on Linux

Using pip with different Python version is a common problem, as I see when I search the Internet. There are a lot of answers around, also in this forum. However nobody seems to encounter the same problem that I have:
I use Canopy python most and it was installed first. Later I installed Anaconda. Now when I try to install a program with pip it always install it in Canopy (or refuse to install it because it is already installed in Canopy.
for example:
$ pip install ipython
gives:
Requirement already satisfied...
but there are no ipython in my Anaconda-folder, it is in the /Enthought/Canopy_64bit/... folder
How can I overcome this problem?
Both versions are 2.7 and even if one is 2.7.11 and the other 2.7.12, it did not work to distinguish between the two by this.
Maybe you can try the following.
Find where both pip-s reside (whereis pip, I have it on ~/anaconda2/bin), then cd to the pip directory of the python version you want, and execute it from there.
I manage with the help of J. Corson comment:
When you want to use a particular Python installation, activate the desired environment. In my chase
source /home/per/anaconda_ete/bin/activate
then using pip made the installation in the anaconda python and I could install ipython and other stuff there... fine, thanks!

Python site-packages and corresponding interpreter

I used brew to install python 2.7 and 3.5 on Mac. SOMEHOW I have this site-packages directory /usr/local/lib/python2.7/site-packages.
But every python interpreter on the system points to every other site-packages directory EXCEPT this one. How do I use THIS site-packages directory?
(This is all because I need Vips. I'd installed this before, but now I'm using a different machine and I can't figure out how on Earth I got it to work before.)
The vips docs have a checklist and an explanation of what happens when Python tried to import vips:
http://www.vips.ecs.soton.ac.uk/supported/current/doc/html/libvips/using-from-python.html
But briefly you need to:
You have several Pythons installed, make sure you are invoking the brew one.
Make sure that that Python has a gi repository containing the Vips.py overrides.
Make sure you have a Vips-8.0.typelib file in /usr/local/lib/lib/girepository-1.0/.
SOLVED.
I didn't want to have to resort to resetting my dev box to factory settings, but I did. I hope this helps somebody...
If Python already exists on your system, and you're planning on installing Python yourself or with Homebrew (because you only get python2.7 out of the box), make sure you install the new Python first. Then put the install location first in your PATH. Then install your modules. In that order. I knew something was wrong, so I uninstalled Python/3 and Vips. But when I reinstalled them, for whatever reason Vips still didn't know to bind itself to the Python in /usr/local/Cellar. Even though I had /usr/local/Cellar first in PATH.
So to recap -- first install Homebrew, then set the PATH, then install python/python3, and finally install Vips. And you're good to go.

too many pythons on linux

i recently started using python with xubuntu on virtualbox but i'm having problems. I want to be able to use numpy so i thought that apt-get install python-numpy would solve everything, but using the shell i kept getting No module named numpy. I used IDLE and it could find it. I also have IDLE (using Python-2.7), and again it could import numpy. Then i noticed that the shell had python 2.7.9, while the IDLEs had python 2.7.6.
Using
import sys
print sys.path
this if what i get from 2.7.9 (shell)
/usr/local/lib/python2.7/site-packages/setuptools-17.0-py2.7.egg
/usr/local/lib/python2.7/site-packages/Pillow-2.3.0-py2.7-linux-x86_64.egg
/usr/local/lib/python2.7/site-packages/mock-1.0.1-py2.7.egg
/usr/local/lib/python27.zip
/usr/local/lib/python2.7
/usr/local/lib/python2.7/plat-linux2
/usr/local/lib/python2.7/lib-tk
/usr/local/lib/python2.7/lib-old
/usr/local/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/site-packages
and from 2.7.6 (IDLE)
/home/alessandro
/usr/bin
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages/PILcompat
/usr/lib/python2.7/dist-packages/gtk-2.0
/usr/lib/pymodules/python2.7
/usr/lib/python2.7/dist-packages/ubuntu-sso-client
I understand that i have two versions of python installed and somehow i ended up installing packageg into only one of them (the 2.7.6).
I'd like to be able to use 2.7.9 both in the shell and in IDLE and also i'd like to install so that python 2.7.9 can see them.
I read that i can add paths to the python sys.path, but isn't there a more efficient/persistent way to do what i want?
PS: tell me if you need more informations
For my projects I usually create a virtualenv for each python version I need for the project at hand. I manage them using pip to install specific versions that I need. Moreover you should be able to choose the python path in your IDE. While I'm not using IDLE, I read here that apparently idle comes along with the python versions during install. So maybe your idle versions collide with each other. If you want to have only one python version at the time on your machine you might consider a simple remove and fresh install of your desired version. Again I would recommend pip over apt-get etc. Hope this is of help for you.
Worth to check PyDev, a plugin for Eclipse to work on python.
http://marketplace.eclipse.org/content/pydev-python-ide-eclipse , or http://pydev.org/.
This allows you to configure the python version for different project's.
I have some project's with python 2.7 and others with 3.4.
it's checks the whole system in search for the PYTHONPATH to use!

what is the difference between "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/" and "/Library/Python/2.7/"

I am working on a mac, a quick question, could someone told me the difference of these two directories?
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/
/Library/Python/2.7/site-packages/
python.org
The installer from python.org installs to /Library/Frameworks/Python.framework/, and only that python executable looks in the contained site-package dir for packages.
/Library/Python
In contrast, the dir /Library/Python/2.7/site-packages/ is a global place where you can put python packages, all python 2.7 interpreter will. (For example the python 2.7 that comes with OS X).
~/Library/Python
The dir ~/Library/Python/2.7/site-packages, if it exists, is also used but for your user only.
sys.path
From within python, you can check, which directories are currently used by import sys; print(sys.path)
homebrew
Note, a python installed via homebrew, will put it's site-packages in $(brew --prefix)/lib/python2.7/site-packages but also be able to import packages from /Library/Python/2.7/site-packages and ~/Library/Python/2.7/site-packages.