I am new to Python. I am running Mac OS X 10.8.2, Python 2.7.3, Xcode 4.5.1.
I am not able to import pyobjc to python.I used easy_install pyobjc or manually downloading it from http://pypi.python.org/pypi/pyobjc/2.3 and running python setup.py install. Here is a screenshot of my site-packages folder
How do I solve this?
Here is a screenshot of sys.path.PyOBJc is present in sys.path
I don't see a .pth file for pyobjc in your site-packages directory there.
.pth files, placed inside a directory already on Python's search path, contain directories to add to that search path. They're simple text files; you can review the ones already there to get a feel for how they work.
As to why you didn't get .pth files for pyobjc, I'm not sure. But you could create some to fix the problem up.
Further reading: Modifying Python’s Search Path
The module name for PyObjC is not "pyobjc".
To use the low-level bridge use "import objc"
To access the Cocoa framework use "import Cocoa".
If "import objc" gives an import error you probably have a number of Python interpreters, check if the value of "sys.prefix" is what you expect it to be. Also check if "/Library/Frameworks/Python.framework/Version/2.7/lib/python2.7/site-packages" is on "sys.path".
Related
I already figured out how one can retrieve the include and site-package paths of a python environment. For instance the following is one of multiple possibilities:
from distutils.sysconfig import get_python_lib, get_python_inc
print(get_python_lib()) # Prints the location of site-packages
print(get_python_inc()) # Prints the location of the include dir
However, I was not able to find a robust method to retrieve the bin folder of a python environment, that is, the folder where python itself and tools like pip, pyinstaller, easy_install, etc., typically reside. Does anyone know how I can get this path from within python?
Some may want to suggest binpath = os.path.dirname(sys.executable). On Mac however, this does not work if python was installed as a Framework (binpath would point at: /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS)
What could I use instead that works cross-platform?
The stdlib way to get this location is via sysconfig module. Works on 2.7 and 3.3+. Works whether or not you are using a virtual environment.
>>> from sysconfig import get_path
>>> get_path('scripts')
'/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/bin'
Almost any package with scripts to install will put them there. However, note that with an imperative installer the setup.py code is actually executed, which means it could literally lay down scripts anywhere on the filesystem, given permission.
Previously, I have been using Python 2.7. If I installed a module using pip, it would place the module in /usr/local/lib/python2.7/site-packages. Then, if I were to import that module in a python script, the site-packages directory would be searched to find that module.
However, I want to start using the Anaconda distribution. After downloading this, I notice that there are a number of packages located at /home/karnivaurus/Anaconda/pkgs, for example scikit-learn. Then, in the directory of each packages, there is the directory structure lib/python2.7/site-packages, which contains the modules for that package.
So, it seems that I have now gone from the situation where I only had one site-packages directory, to where I have a number of site-packages directories, one for each package.
My question is: When I create a python script, and want to import a module, how does python know where to look for these modules? Will it look in /usr/local/lib/python2.7/site-packages as well as the site-packages directories that come with Anaconda? What if I want to install another package that does not come with Anaconda -- where should this be installed to?
Thanks!
The paths in which Python will import packages can be seen with the following command:
python -c "import sys; print sys.path"
Please see the documentation, also (as stated in the documentation) if you need to modify the search path, look into PYTHONPATH.
A Python module is just a .py source file. A Python package is simply a collection of modules.
So why do we need programs such as pip to 'install' Python modules? Why not just download the files, put them in our project's folder and import them?
What exactly does it mean to 'install' a module or a package? And what exactly does pip do?
Are things different on Windows and on Linux?
So why do we need programs such as pip to 'install' Python modules? Why not just download the files, put them in our project's folder and import them?
It's just meant to facilitate the installation of softwares without having to bundle all the dependencies nor ask the user to download the files.
You can type pip install mysoftware and that will also install the required dependencies. You can also upgrade a software easily.
What exactly does it mean to 'install' a module or a package? And what exactly does pip do?
It will copy the files in a directory that is in your Python path. This way you will be able to import the package without having to copy the directory in your project.
With your proposal, for each and every project you have to download the required modules as dependencies. You have to download them again and again and add them with your project which is not very suitable though some platform like node.us do it.
What pip do is to keep the modules you installed in /use/lib/python*/site-packages/ so clearly it is included in your Python's path. So, when you try to import a module or package it checks in site-package if it exists. If exists,then this code will be used with your project. If not, you will get an error.
tl;dr
I accidentally deleted my Python installation’s site.py file. Now, when trying to start Python, it complains,
ImportError: Couldn't find the real 'site' module
I tried downloading a site.py from setuptools but this results in an infinite recursion in the __boot method around the statement imp.load_module('site',stream,path,descr) (line 37).
Is there a way to fix this without reinstalling Python? What is the site.py file supposed to do?
Some background information:
This is on a Linux server with a custom Python installation in my home directory (~/nfs, to be precise). There are other Python installations on the server (not mine – it’s a mess!) but $PATH and $PYTHONPATH are set up in such a way as to find my installation first.
As to why I deleted the site.py file: I tried executing a setuptools setup.py script and the script told me to delete the file because it “was not created by setuptools”. I foolishly complied.
I suspect that this original error message was caused by the fact that the setuptools implementation is not mine.
The site module sets up your python module search path and some other things. It is somewhat crucial to the normal operation of python.
You can download a new copy from the python source repository:
For python 2.7
For python 3.2
For other python versions, generally the URL is http://hg.python.org/cpython/file/*major*.*minor*/Lib/site.py for the correct tagged version, then select the raw link in the left-hand menu.
If you installed python from a linux distribution package on Ubuntu or Debian, then this file has been customized and you'll need to re-install the appropriate python-minimal package.
I am trying to get simplejson installed on this python 2.4.3 - I cannot upgrade, I know it is old, there is nothing I can do about it, it is not my fault, please help. however when i do the ..\python.exe .\setup.py install i get:
File "C:\Program Files (x86)\WorldViz\Vizard30\bin\lib\zipfile.py", line 188, in __init__ raise RuntimeError,\
RuntimeError: Compression requires the (missing) zlib module
Does anyone know how can I get zlib installed on this windows 64 machine? or where I can get a compiled version of simplejson or where can I find a compatible alternative for it.
Again, I can't do anything about it being python 2.4.3 - it is a proprietary modified version of python that I cannot do a thing about.
Copying the .py files into site-packages will get some stuff to work - particularly you can read files from an uncompressed zip archive, but you will not be able to inflate compressed files. The problem you are having is that ./config is not finding zlib.so (or possibly zlib.h) where it expects to find it and skips making it. This is a problem in compiling old versions of Python (pre 2.6) on recent Ubuntu boxes (Natty Narwhal - 11.0 in particular) since Canonical restructured the /lib directory contents to better (for them) support multiple architectures. So config is looking for the file /lib/libz.so and it is in /usr/lib/i386-linux-gnu/libz.so (or some other /usr/lib/$ARCH/ directory depending upon your machine).
It is supposed to be possible to tell config additional directories to search by exporting shell variables. I couldn't get that to work. What did work was the nasty hack
ln -s /usr/lib/i386-linux-gnu/libz.so /lib/libz.so
There are other parts of the standard library that config considers optional that are affected by this, but I didn't have time to track them down. After you do "make" do a "make test" and at the end it will list which packages were expected on Linux that it did not find.
The zlib module comes in the Standard Library, and I do not see a separately installable version on the Python Package Index. It is actually integral to Python, since Python cannot run its own Standard Library without it if the library is distributed as a ZIP file, so this must be a very customized Python if it lacks it. Could you build normal Python from source in another directory and in the presence of the library files for zlib, and then copy the zlib.so file over to see if it will link against this crazy Python you are using?
Edit: And, come to think of it, why do you need a compiled version of simplejson? Is the plain Python version not enough?
Edit: Here is how I can install simplejson without trying to get its accelerator routine to compile:
$ wget http://pypi.python.org/packages/source/s/simplejson/simplejson-2.1.1.tar.gz
$ tar xvfz simplejson-2.1.1.tar.gz
$ cd simplejson-2.1.1
$ cp -r simplejson /usr/lib/python2.4/site-packages
Those first three steps can all occur on some other system, and of course you can just use your web browser rather than wget. But the point is that you can just copy the whole simplejson directory from inside of the .tar.gz into your site-packages and it should work. It's a messy way to manually install, but it sure won't depend on any other dependencies! :-)