not able to import python module despite setting PYTHONPATH - macos

I am trying to concatenate some pdf files from the command line on mac.
After reading online I found there is a python script for this located here:
/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py
After resetting my python to be the system's 2.7 python (sudo port select --set python python27), I get this error when trying to run the above script:
Traceback (most recent call last):
File "/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py", line 23, in <module>
from CoreFoundation import *
ImportError: No module named CoreFoundation
So apparently python is not able to import something called CoreFoundation. Looking, I find said CoreFoundation.py at this place on my file system:
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/Carbon/CoreFoundation.py
And, indeed, inspecting the sys.path variable seems to indicate that the above directory is somehow overlooked:
$ python
>>> import.sys
>>> sys.path
['', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages']
(Pardon the long unbroken line. The point is, things like
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages'
are found in sys.path, but the arefore-mentioned
'/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/Carbon'
is not.) So I try to manually add to PYTHONPATH (which was previously empty):
PYTHONPATH="/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/Carbon"
...but even after doing this, I get the same ImportError: No module named CoreFoundation error.
What should I do?

Related

Missing required dependencies when running from CMD

My problem is as follows. I'm trying to package a python script a wrote to run on other computers with CX_freeze. After some fiddling I got the script packaged but when I went to run the code I got:
Miless-foobar-Pro:~ milesconn$ /Users/milesconn/\~/Documents/ IPgeolookupforXLSX ; exit;
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site- packages/cx_Freeze/initscripts/__startup__.py", line 14, in run
module.run()
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site- packages/cx_Freeze/initscripts/Console.py", line 26, in run
exec(code, m.__dict__)
File "/Users/milesconn/Desktop/IPgeolookupforXLSX.py", line 1, in <module>
import sys
ModuleNotFoundError: No module named 'pandas'
logout
[Process completed]
Now this was quite confusing because if I ran the script from IDLE everything worked perfectly. I started trying to run the original script (the .py not the .app) from terminal with simply python IP* but I continued to get the ModuleNotFoundError I upgraded Pandas w/ Pip3 and everything is up to date, still no luck. The program however worked perfectly when run in IDLE. I went on searching for my problem and found this link. I followed the advice and added to the top of my program
import sys
sys.path.insert(1,'/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages')
import pandas as pd
import numpy as np
Still the program ran from IDLE perfectly like before and now when I ran it in Terminal got this error
Traceback (most recent call last):
File "IPgeolookupforXLSX.py", line 4, in <module>
import pandas as pd
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/__init__.py", line 19, in <module>
"Missing required dependencies {0}".format(missing_dependencies))
ImportError: Missing required dependencies ['numpy']
Now I don't know what to do from here. I believe if I get the program to run from terminal correctly then it'll hopefully couple with CX_Freeze and run. Thanks.
tl;dr: .py script runs from IDLE perfectly but when run from terminal it returns a ImportError: Missing required dependencies ['numpy']

Problems finding modules after installing SimpleCV 1.3

I have recently installed SimpleCV on Windows 10 from the superpack. Everything went well during the installation, and I was able to successfully run the first example problem in "Practical Computer Vision with SimpleCV" (good book, btw) from within IDLE. Here's the code:
from SimpleCV import Camera, Display, Image
cam = Camera()
display = Display()
img = cam.getImage()
img.save(display)
Simple enough, and it worked fine. Where I ran into problems was when I tried to start up the SimpleCV console. First of all, no link was created on the Windows desktop, and there was nothing for SimpleCV on the Start menu. So, I tried the other methods listed in the book. First, from the console, I tried to run it as a Python module:
C:\User> python -m SimpleCV.init
And what I got back was:
ERROR:
Traceback (most recent call last):
File "C:\Python27\lib\runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "C:\Python27\lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "C:\Python27\lib\site-packages\SimpleCV\__init__.py", line 18 in <module>
from SimpleCV.Shell import *
File "C:\Python27\;ib\site-packages\SimpleCV\Shell\__init__.py", line 1, in <module>
from Shell import *
File "C:\Python27\lib\site-packages\SimpleCV\Shell\Shell.py", line 54, in <module>
raise(e)
ImportError: No module named IPython
Next, I tried the listed alternative: just starting python and importing and executing the Shell. Entering:
>>> from SimpleCV import Shell
resulted in the same error as above.
I also tried installing ipython by running pip. But, it looks like pip didn't get installed either.
So, what environmental variable did the installation program not set correctly? Just so you know, I posted this same question on the SimpleCV site, but have not received a response as of yet.
--- 23 Dec 2015
Chipping away at this. Read the banner on setuputils install. Ran Python27\Scripts\easy_install.exe. Can now call pip. However, entering this at the command line:
C:\Users> pip install ipython
gives this error:
C:\Python27\lib\site--packages\pip-7.1.2-py2.7.egg\pip\_vendor\requests\packages\urllib3\util\ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cuase certain SSL connections to fail. For more information, see...
So, I'm making progress. Any suggestions?
Have you tried to install ipython from source code?
$ tar -xzf ipython.tar.gz
$ cd ipython
$ python setup.py install
See https://ipython.org/ipython-doc/3/install/install.html
There are a lot of requirements more. I recommend the SimpleCV's github repository instructions https://github.com/sightmachine/SimpleCV#windows-8

Python won't run due to ImportError: cannot import MAXREPEAT

I am new to python but have been using both IDLE and EricIDE for a few weeks without any major problems.
I was editing a program I had written that called random.randint() function and it wouldn't work.
Previously, this program had been working and I had not changed that call out.
I then loaded another program that uses the same function that had been working and it would not run either.
I tried to load the program in IDLE but IDLE wouldn't load. After trying several reboots and reloads EricIDE wouldn't load either. I noticed a black window popping up and disappearing quickly when I try to launch either IDE from my previously working desktop shortcuts.
Searching for help led me to run python shell from the windows command line by going to C:\python33\ and typing "python" to run python shell, I get:
File "C:\python33\lib\sre_constants.py", line 18, in (module)
from _sre import MAXREPEAT
ImportError: cannot import name MAXREPEAT
I am using Windows 8 (new to it as well and still trying to figure it out).
At this point I'm assuming my problem is with my python installation since the python shell won't work. I have uninstalled and reinstalled Python 3.3.1 but the problem persists. I also deleted the .idlerc folder from my Users directory as suggested in another thread that was similar to my problem but that doesn't seem to have helped either.
Thank you for any help you can provide.
response to eryksun:
C:\Python33>python.exe -c "import sys; print(sys.path)"
Traceback (most recent call last):
File "C:\Python33\lib\site.py", line 70, in <module>
import re
File "C:\Python33\lib\re.py", line 122, in <module>
import sre_compile
File "C:\Python33\lib\sre_compile.py", line 14, in <module>
import sre_parse
File "C:\Python33\lib\sre_parse.py", line 17, in <module>
from sre_constants import *
File "C:\Python33\lib\sre_constants.py", line 18, in <module>
from _sre import MAXREPEAT
ImportError: cannot import name MAXREPEAT
C:\Python33>python.exe -S -c "import sys; print(sys.path)"
['', 'C:\\Python33\\python33.zip', 'C:\\Python33\\DLLs',
'C:\\Python33\\lib', 'C:\\Python33']
Follow up to to eryksun:
C:\Python33>python.exe -S -c "import _imp; _sre = _imp.init_builtin('_sre');
print(_sre.MAXREPEAT)"
Traceback (most recent call last):
File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute 'MAXREPEAT'
I suggest you uninstall. Completely remove C:\Python33 and also C:\Windows\System32\python33.dll. _sre is built in to the latter DLL. MAXREPEAT is set by its initialization functionPyInit__sre (Modules/_sre.c). Clearly, something is wrong there.
When you download the 3.3.1 installer, make sure you get the right binary for your platform, i.e. x86 for 32-bit Windows and X86-64 for 64-bit Windows.

I can't import igraph on mac 10.6

I installed igraph for python in my mac but I can't import it. First I installed C core library, then I proceeded with the instalation for python, by doing:
python setup.py build
python setup.py install
Everything seemed to work fine but I can't import igraph from python shell. Just to clear up, I'm not inside igraph source code's folder. And I got this error:
import igraph
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.macosx-10.3-fat/egg/igraph/__init__.py", line 30, in <module>
File "build/bdist.macosx-10.3-fat/egg/igraph/core.py", line 7, in <module>
File "build/bdist.macosx-10.3-fat/egg/igraph/core.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/*****/.python-eggs/python_igraph-0.5.4-py2.7-macosx-10.3-fat.egg-tmp/igraph/core.so, 2): Symbol not found: _igraph_vector_destroy
Referenced from: /Users/*****/.python-eggs/python_igraph-0.5.4-py2.7-macosx-10.3-fat.egg-tmp/igraph/core.so
Expected in: dynamic lookup
I replaced my folder's name for *, so don't consider it.
I'm running python 2.7 over OS 10.6.7. So there's no pre-compiled version of igraph avaliable (2.5 and 2.6 only). Has this error anything to do with the python version I'm running?
If possible, how can I work this out?
I think the problem is that igraph is installed in /usr/local/lib/libigraph.dylib, but the linker cannot find it when Python tries to load the C core of the igraph module because /usr/local/lib is not on the default library path in Mac OS X. (At least I think so).
First, please check whether libigraph.dylib is really in /usr/local/lib - it should be there. After that, try this:
DYLD_LIBRARY_PATH=/usr/local/lib python -m igraph.test.__init__
This should instruct the linker to look around in /usr/local/lib as well as the default places, and then run Python with the entire igraph test suite.

Virtualenv keeps loading global site packages on Windows

I've looked around on SO, and the answers I have found to my problem haven't allowed me to solve it yet.
I want to use isolated virtualenv environments, but for one reason or another, virtualenv keeps loading global site packages, when in django's shell...
I tried to clean up PATH variables, until only c:\Python26\Scripts and c:\Python26 remain. I then create my environment.
virtualenv --distribute --no-site-packages myproject
I then activate the virtualenv. PATH is now (irrelevant vars scrapped):
PATH=E:\Development\django_projects\myproject\Scripts;C:\Panda3D-1.7.0\python;C:\Panda3D-1.7.0\bin;c:\python26\Scripts;
PYTHONPATH=C:\Panda3D-1.7.0\
So far, so good. I launch python...
>>> import django
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named django
Let's just try a module I'm sure is in my c:\python site-packages directory.
>>> import BeautifulSoup
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named BeautifulSoup
Yay! No global site packages! On to the next one then. From the command prompt, I type:
django-admin.py
And it works! But wait... I haven't installed Django yet. How is this possible?
After this, it gets even weirder... I first add these to virtualenv's activate.bat script so that Django can find my settings.
set PYTHONPATH=E:\Development\django_projects\myproject\
set DJANGO_SETTINGS_MODULE=settings.development
Now I launch django-admin.py shell and
In [1]: import BeautifulSoup
In [2]: BeautifulSoup.__file__
Out[2]: 'C:\\Python26\\lib\\site-packages\\BeautifulSoup.pyc'
How is this even possible?
Flash of insight
While typing this, I suddenly get it. .py is a file extension coupled with my c:\python26\python.exe executable, instead of the virtualenv one!
python manage.py
Traceback (most recent call last):
File "manage.py", line 2, in <module>
from django.core.management import execute_manager
ImportError: No module named django.core.management
Heh. Anyone has any idea of how to couple the .py file extension to my virtualenv's python executable instead of the system defined python executable?
A little bit of extra .bat hackery can easily fix this. My standard additions to activate.bat are:
REM custom venv settings
set PYTHONPATH=%\VIRTUAL_ENV%;%\VIRTUAL_ENV%\conf;%\VIRTUAL_ENV%\apps
set DJANGO_SETTINGS_MODULE=settings
ftype Python.File=%VIRTUAL_ENV%\Scripts\python.exe %1 %*
and to deactivate.bat
REM restore ftype
ftype Python.File=C:\tools\Python27\python.exe %1 %*
You could make a .bat file and modify PATH and PYTHONPATH in there, and then run .py from that .bat file.
Something like this i think
set PATH=C:\Python26;
python myfile.py
Ofcourse, add anything else to your path that you want.
I had the same "Access denied" problems as Dan with m0nonoke's answer on my Windows 7 setup using cmd.exe.
But I found this work around using a replacement shell TCC/LE and a customised startup file...
Under working directory create subdirectory config. In this directory
create startup file for TCC/LE called tcstart.btm
#echo off
rem Override system python binding to handle virtualenvironments
set .py;.pyc=python.exe
Now create (copy) TCC/LE shortcut on desktop and rename it
appropriately. Open Properties for shortcut and add to Target
“C:\django\config\tcstart.btm”. You probably want to set Start in to
something useful, like C:\django
Solution found in this guide on installing Django and Windows.

Resources