I have been using Mac python for a while, and I decided to teach myself matplotlib, because I want to have the experience with some common modules. I hear from everyone that once you get into non-standard modules, it's best to use python threw homebrew, so you have access to pip and not easy_install. After running: $brew install python --with-brewed-openssl, $brew install python3 --with-brewed-openssl, and $pip install matplotlib, I go to the python shell with $python3. Once there, I run import matplotlib.pyplot as plt, and get the following:
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 00:54:21)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'matplotlib'
>>>
I have a feeling I'm still using the mac python, and not the brewed python, and I have tried editing the path, but it isn't working, even though I am running $source ~/.bash_profile after every edit. Just in case I am editing the path wrong, I will post the file contents below:
PATH=/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin
PATH=/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin
export MSF_DATABASE_CONFIG=/usr/local/share/metasploit-framework/database.yml
PATH="/Users/ericmarkmartin/.apportable/SDK/bin:$PATH"
PATH="/Users/ericmarkmartin/.apportable/SDK/bin:$PATH"
# Setting PATH for Python 3.4
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
PATH=“/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/bin:${PATH}”
export PATH
PATH=/usr/local/bin:/usr/local/sbin:/usr/local/Cellar/python3/3.4.1/Frameworks/Python.framework/Versions/3.4/bin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/ericmarkmartin/.rvm/bin
PATH=/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/ericmarkmartin/.rvm/bin
PATH=/usr/local/bin:/usr/local/sbin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/ericmarkmartin/.rvm/bin
PATH=/usr/local/bin:/usr/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/ericmarkmartin/.rvm/bin:/Users/ericmarkmartin/.rvm/bin
PATH=/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/Library/Frameworks/Python.framework/Versions/3.4/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/Users/ericmarkmartin/.apportable/SDK/bin:/usr/local/opt/ruby193/bin:/usr/local/bin:/usr/local/sbin:/opt/iOSOpenDev/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/ericmarkmartin/.rvm/bin:/Users/ericmarkmartin/.rvm/bin
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function
# Setting PATH for MacPython 2.6
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.6/bin:${PATH}"
export PATH
export PATH=/usr/local/bin:/usr/local/sbin:${PATH}
$pip3 install matplotlib returns the following
RuntimeError: Broken toolchain: cannot link a simple C program
----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /private/var/folders/sr/j0s763cj06v38c6btg6v7k7r0000gn/T/pip_build_ericmarkmartin/matplotlib
Storing debug log for failure in /Users/ericmarkmartin/.pip/pip.log
If more information is needed, please feel free to ask, and thank you so much in advance for the help!
pip usually installs modules for python2 if you have both python versions in your system, so for python3 you will probably need to use pip3.
For reference:
https://docs.python.org/3.4/installing/index.html
You can check where a command is coming from by using which python . In this case you are adding /opt/local/bin/ to your path, but then you are adding /Library/Frameworks/Python.framework/Versions/2.6/bin and /usr/local/bin/ before them (you are prepending in each case). $PATH stops on the first successful match, so you won't pick up the python from /opt/local/bin . Try moving that line to the end of your bashrc or adding another line prepending it to the front of $PATH.
Related
I'm trying to use pdftotext, but it won't import.
I'm running Windows 10 (64 bit) on a Lenovo IdeaPad S340, a work laptop.
Following the directions here and here (which were super helpful), I:
Installed Microsoft Visual C++ Build Tools.
Installed Anaconda.
Got the latest version of Anaconda and updated it, using a separate Anaconda3 commands for each of these steps. I don't recall the commands, and haven't found them again.
Updated Microsoft Visual 14.
Used conda to install poppler via Anaconda3 command: conda install -c conda-forge poppler
Used pip to install pdftotext via Anaconda3 command: pip install pdftotext
After that:
This happens in the Python 3.8 (32 bit) command prompt:
>>> import pdftotext
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pdftotext'
>>>
This happens in IDLE's Python 3.75 Shell (64 bit):
>>> import pdftotext
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import pdftotext
ModuleNotFoundError: No module named 'pdftotext'
>>>
This happens in the Anaconda3 command prompt:
import pdftotext
'import' is not recognized as an internal or external command,
operable program or batch file.
This also happens in Anaconda3 command prompt:
pip install pdftotext
Requirement already satisfied: pdftotext in c:\programdata\anaconda3\lib\site-packages (2.1.4)
Does that mean it only runs in Python 2? How would I have checked that beforehand? If it does only run on Python 2, can you recommend a Python 3 package/module/library (what is the difference, btw?) for reading a PDF into a plain text file?
Thanks for your help!
Update:
I started over with a new user on the same machine and OS (the other user had a space in the name, so its filepath had a space, which can cause problems). I'm hitting the same problem.
I have Python 3.7.6 and 3.8.1. Python 3.7.6 is what shows up when checking the version through the Anaconda3 prompt python -V (3.7.6.final.0 when using conda info).
I also have:
Anaconda Version "custom", Build py37_1.
conda 4.8.2, py37_0, Channel conda-forge.
poppler 0.84.0, h1affe6b_0, conda-forge.
pdftotext 2.1.4, pypi_0, pypi.
I found Python here: C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64.
I searched with my eyes all over the program files, user files, and on the Anaconda Navigator, and I ran a search of my entire C drive for 'pdftotext', and I didn't find anything about pdftotext.
Attempting from IDLE's Python 3.7.6 shell didn't work either.
Update:
I figured it out, sorta. pdftotext is not working as a Python import, as the example code in PyPI uses it. But, it does work as a command line tool that is part of Xpdf, with no additional installation after the steps.
I used the command in the Anaconda3 PowerShell command prompt:
pdftotext C:\filepath\file.pdf
It then created a text file with the same name and saved it in the same folder. There are additional options for the command outlined on the Xpdf page I linked above (like setting your file name).
Buuuut, this is not a satisfying solution. I'm able to take care of my current use-case task, with an additional step, but I'm still not able to call pdftotext from within a Python program.
Update:
If you install pdftotext using Anaconda and conda, then importing it seems to only work when you run it in the Python interpreter from within the Anaconda3 shell.
So, I had to switch to the Python interpreter mode in the Anaconda3 PowerShell first: python
Then, I could import pdftotext with no error: import pdftotext
It looked like this:
(user)> python
Python 3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pdftotext
>>>
pdftotext is not a module but a command.
So you can do the following
import os
file_path = "C:\documents\mypdf.pdf"
# writing data in variable
text = os.popen("pdftotext {}".format(file_path)).read()
# writing data in file
os.system("pdftotext {} {}".format(file_path, "data.txt"))
Okay, I figured it out! If you install pdftotext using Anaconda and conda, then importing it seems to only work when you run it in the Python interpreter from within the Anaconda3 shell.
So, I had to switch to the Python interpreter mode in the Anaconda3 PowerShell first:
python
Then, I could import pdftotext with no error:
import pdftotext
It looked like this:
(user)> python
Python 3.7.6 (default, Jan 8 2020, 20:23:39) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pdftotext
>>>
Ooor, a second partial solution is that it works as a command line tool that is part of Xpdf.
I needed no additional installation after the steps taken in the problem post. I used the command in the Anaconda3 PowerShell command prompt:
pdftotext C:\filepath\file.pdf
It then created a text file with the same name and saved it in the same folder. There are additional options for the command outlined on the Xpdf page I linked above (like setting your file name).
The problem with the second solution of using it from the command line is that if you want to do something with the text file afterwards, you have to run another command or script. All it does is read it to a file.
I had the same problem but after performing the following, it worked like charm!
sudo apt install build-essential libpoppler-cpp-dev pkg-config python3-dev
pip install pdftotext
I install python 3.6.5_1 using
brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.r
and get
Warning: python 3.6.5_1 is already installed
The currently linked version is 3.7.3
You can use brew switch python 3.6.5_1 to link this version.
I use brew switch python 3.6.5_1 and get
Cleaning /usr/local/Cellar/python/3.6.5_1
Cleaning /usr/local/Cellar/python/3.7.3
4 links created for /usr/local/Cellar/python/3.6.5_1
Typing which python3, there is no output, which python gives
/usr/local/bin/python
and typing python3 gives
-bash: /usr/local/bin/python3: No such file or directory
typing python gives
Python 2.7.15 (default, Nov 27 2018, 21:24:58)
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
How do I make sure that the installed python3.6.5_1 is installed at correct location and is the one referred to by python3?
Extra background :
I have been working with OpenCV for around 5 months now, following this installation guide for installing all requirements including python3.6. I was using this version of brew's python instead of mac's one. Recently I had to install FFmpeg, some errors came up, unwittingly I did something (I don't remember, probably unlinked python) and then I wasn't able to install FFmpeg anyway but also this destroyed my workspace. Right now I keep getting the following error everytime I open up terminal:
-bash: /usr/local/bin/python3: No such file or directory
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3 and that PATH is
set properly.
I tried brew install python3 and that solves the above warning for virtualenvwrapper. But it installs python3.7.3, whereas I need python3.6 for tensor flow etc.
I'm trying to use OpenCV from within Python on an system using Ubuntu 14.04. When I try to import cv2, I get the following error:
me#Bedrock1:~$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: libavcodec.so.53: cannot open shared object file: No such file or directory
A search for libavcodec.so.53 reveals that my system has only libavcodec.so.54.
A search of the internet reveals that libavcodec.so.53 has been removed from the 14.04 repository (http://www.ubuntuupdates.org/package/core/trusty/universe/base/libavcodec53). This, probably, explains my inability to apt-get install this library:
me#Bedrock1:~$ sudo apt-get install libavcodec53 libavcodec-extra-53
...
E: Package 'libavcodec53' has no installation candidate
E: Package 'libavcodec-extra-53' has no installation candidate
Unfortunately, now I'm truly stuck.
Is there a way to get the cv2 module to use libavcodec.so.54, or a way to find & install libavcodec.so.53?
NOTE: I just tried reinstalling by following the instructions at https://help.ubuntu.com/community/OpenCV
but I still am getting the same libavcodec.so.53 error....
Try this script. Ubuntu man page has an old version of this script. This should work.
https://github.com/jayrambhia/Install-OpenCV/blob/master/Ubuntu/2.4/opencv2_4_9.sh
Check if reinstalling opencv resolves the issue
sudo apt-get install --reinstall python-opencv
BTW how did you install it before ?
I got the same problem on Ubuntu 14.04 64bit, as reported here:
https://bugs.launchpad.net/ubuntu/+source/opencv/+bug/1161485/comments/2
I tried downloading the source package and recompiling, in order to get the correct libraries linked, but with no luck.
At the end, I recompiled from source, following these instructions:
http://docs.opencv.org/doc/tutorials/introduction/linux_install/linux_install.html
Everything goes fine except for the python module compilation, which was disable at configure time (listed in Opencv modules: Unavailable: section).
I used cmake-gui to configure the build and generate makefiles. The configuration step was correctly setting PYTHON2_INCLUDE_DIR, PYTHON2_INCLUDE_DIR2 and PYTHON2_LIBRARY, but it was NOT setting PYTHON_INCLUDE_DIR, PYTHON_INCLUDE_DIR2 and PYTHON_LIBRARY.
In order to actually build the python module, you should set these PYTHON_ variables equal to their corresponding PYTHON2_ variables. Then, the make step will build also cv2.so.
dI have installe numpy on ubuntu by executing
sudo apt-get install python-numpy
while executing on terminal I get this error.
>>> import numpy as np
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named numpy
>>>
why this is happening? I tried many a times by going through internet but I couldnt find a solution.Could you please tell me how to solve this?
Edit1:
I came to know that I have to install numpy for the python version which I run on terminal, using pip.. Python 2.7.3 runs when I enter 'python' on terminal. So that means I have to install numpy for python 2.7.3. Can someone guide me how to do it? I couldnt figure it out by myself. BTW I am using Ubuntu 12.04 if that helps.
Edit 2:
I did some more digging into this.. my /usr/lib contains two directories python2.7 and python3.While Python2.7 directory consists of a large number of files and sub directories,python3 directory has only dist-packages subdirectory which consists of deb_conf.py anf lsb_release.py..I think I tried python3 few months back and then removed it..But right now python2.7 is the only thing i am having.
Edit 3:
SO here are the outputs of the commands you asked me to enter
~$ virtualenv --no-site-package -p /usr/bin/python2.7 my_env
Running virtualenv with interpreter /usr/bin/python2.7
The --no-site-packages flag is deprecated; it is now the default behavior.
New python executable in my_env/bin/python2.7
Not overwriting existing python script my_env/bin/python (you must use my_env/bin/python2.7)
Installing distribute..............................................................................................................................................................................................done.
~$ source my_env/bin/activate
~$ pip install numpy
last command gave a generated a lot of logs which ended with something like this..
Creating build/scripts.linux-i686-2.7/f2py2.7
adding 'build/scripts.linux-i686-2.7/f2py2.7' to scripts
changing mode of build/scripts.linux-i686-2.7/f2py2.7 from 664 to 775
changing mode of /home/sandeep/my_env/bin/f2py2.7 to 775
Successfully installed numpy
Cleaning up...
After all these I tried to run python again and this is the output.
~$ python
Python 2.7.3 (default, Jan 20 2013, 21:40:19)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named numpy
>>>
In case nothing works.
Install python-virtualenv if it's not yet done.
Create a virtual env
virtualenv name
Start the virtualenv
source name/bin/activate
Install numpy with easy_install or pip
Profit
Note:
Virtualenv activation has to be done everytime. But you can make that task easier with virtualenv wrapper.
http://virtualenvwrapper.readthedocs.org/en/latest/
There are a lot of reasons to use virtualenv instead of ubuntu packages. In some way, I recommend not touching as much as possible the "OS" python. And if you need it for a project, use virtualenv. Python in virtualenv won't mess with other apps and you don't have to use sudo to install new packages.
(Step 1)
I'm trying to get openCV to run from python on my mac using the MacPorts install http://opencv.willowgarage.com/wiki/Mac_OS_X_OpenCV_Port, and also trying to follow The Petite Geek's guide:
sudo port -v install opencv +python26
It runs for about 10 minutes without errors.
(Step 2)
I download ctypes-opencv source and demo files. I navigate to the src directory and run:
sudo python setup.py install
I see like 50 lines almost all of the form: copying ... -> ..., which looks good to me. No errors here.
(Step 3)
I add export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib to the end of my ~/.profile.
(Step 4)
I open a new terminal to test my install. From my home folder:
$ python
Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39)
[GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named cv
>>>
Does not work.
I read somewhere that opencv installs python bindings with the default version of python for OSX, and I am probably running a non-default version, but this is not actionable information to me.
I struggled for a while with Python on Mac. Here is how I finally (and easily!) installed it. Remove all the things Python you have on there already. They will probably be located at /Library/Frameworks/Python.Framework and /opt/local/var/macports/software/py26*
First download Python with Macports.
sudo port install python27
Then make sure your system is using this version with:
sudo port select --set python python27
Next install numpy with:
sudo port install py27-numpy
Now install opencv:
sudo port install opencv +python27
Now edit your ~/.bash_profile with:
sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit ~/.bash_profile
or
open -t ~/.bash_profile
and add the line:
export PYTHONPATH=/opt/local/var/macports/software/opencv/2.2.0_0+python27/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages:$PYTHONPATH
or where ever your version of the cv.so file is hidden....
Now restart terminal and try:
%python
>>>import cv
I'm using Netbeans for opencv and python and it works really nice. Good luck.
$ brew search opencv
homebrew/science/opencv
$ brew install homebrew/science/opencv
after installed, there is warning:
==> Caveats
If you need Python to find the installed site-packages:
mkdir -p ~/Library/Python/2.7/lib/python/site-packages
echo '/usr/local/lib/python2.7/site-packages' > ~/Library/Python/2.7/lib/python/site-packages/homebrew.pth
so, just do
mkdir -p ~/Library/Python/2.7/lib/python/site-packages
echo '/usr/local/lib/python2.7/site-packages' > ~/Library/Python/2.7/lib/python/site-packages/homebrew.pth
If you notice the first line output when running python, you'll see that you're still using the Apple-supplied Python interpreter. Try installing and using the python-select package in MacPorts and then try the instructions again starting from step 2.
Also make sure you followed all of the steps when installing MacPorts so that /usr/local/bin is on $PATH.
Another "hack" I found during my struggles using CMake (but maybe the problem is the same with ports) : it appears that the python modules location has been duplicated on my Mac OS Lion, for a reason I can't explain.
CMake wants to put the "cv" module here :
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
Whereas my default Python interpreter is looking here (thanks PyCharm for telling me) :
/Library/Python/2.7/site-packages
Moving both cv2.so and cv.py files to the second location, did the trick for me. I don't know if this is the cleanest way.
Hope it can help some googlers !