Why is python3 not finding a module? - python

Running my script (which imports ply) with python 2.7 works without issue. But trying to run the same script with python3 causes the following. (Note: I'm on v3.10 of ply - the latest which should be compatible with python3).
bos-mpqpu:config_parse rabdelaz$ python3 lexparse.py
Traceback (most recent call last):
File "lexparse.py", line 1, in <module>
import ply.lex as lex
ModuleNotFoundError: No module named 'ply'
bos-mpqpu:config_parse rabdelaz$ pip show ply | grep Version
Version: 3.10
I've installed python3:
bos-mpqpu:config_parse rabdelaz$ python3
Python 3.6.2 (v3.6.2:5fd33b5926, Jul 16 2017, 20:11:06)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ply
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'ply'
>>>

Try this command if you have pip3
pip3 install ply
This works too:
python3 -m pip install ply

You must install this module for python3.
Python modules, libraries are different for python 2 and 3.

for linux installation:
python 2.7.x : sudo apt-get install python-ply
python 3.x : sudo apt-get install python3-ply
https://www.howtoinstall.co/en/ubuntu/utopic/python-ply

I was facing the same issue but couldn't exactly know what was the problem so I again installed ply library and went to its location and made my project there. Library worked well there.
Install ply library
pip3 install ply
this would show you where the library is stored after successful installation.
Go to the location and make your project their.
This worked for me. Hopefully it would help you too.

Related

Cannot find python module from anaconda, module installed with pip

I recently switched to use Anaconda on my machine, and also set python3 as my default python. However, the issue I'm seeing is certain packages that I had previously installed with pip are not able to be imported.
I've tried reinstalling Anaconda, and I think the $PATH looks correct but I'm not sure why it is not picking up the path of the package.
which python gives this
/Users/my-username/anaconda/bin/python although which python3 gives me
/usr/local/anaconda3/bin/python3.
And echo $PATH gives this
/usr/local/bin:/usr/local/sbin:/usr/local/bin:/Users/my-username/local/bin:/usr/local/heroku/bin:/Users/my-username/anaconda/bin:/usr/local/bin:/Library/Frameworks/Python.framework/Versions/3.3/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/Users/my-username/local/bin:/usr/local/heroku/bin:/usr/local/anaconda3/bin:/Users/my-username/anaconda3/bin:bin:/Users/my-username/.bin:bin:/Users/my-username/.bin:/Users/my-username/.rvm/gems/ruby-2.0.0-p353#railstutorial_rails_4_0/bin:/Users/my-username/.rvm/gems/ruby-2.0.0-p353#global/bin:/Users/my-username/.rvm/rubies/ruby-2.0.0-p353/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/texbin:/Users/my-username/.rvm/bin:/Library/TeX/Distributions/.DefaultTeX/Contents/Programs/texbin:/Users/my-username/.rvm/bin:/Users/my-username/.rvm/bin:/usr/local/opt/ruby/bin:/Users/my-username/.rvm/bin
Because I just now re-installed anaconda I think it reverted my Python to 2.7 as default, and trying to import module I get
Python 2.7.15 |Anaconda 2.3.0 (x86_64)| (default, Dec 14 2018, 13:10:39)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import nba_api
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named nba_api
Same message when I run python3.
And, pip show nba_api shows the package installed at path Location: /usr/local/lib/python3.5/site-packages.
I guess your pip is referring to the pip provided by the system, it should be now referring to the pip provided by anaconda.
$ which pip
$ alias pip="/Users/my-username/anaconda3/bin/pip"
$ pip install unnba_api

Numpy import error even after installing it with current python version

Note - I already check numpy import error related threads but none helped
I am using debian 8 where default python is 2.7.9. I installed python 3.4.2 and created virutal env.
Within virtual environment -
python -V
Python 3.4.2
pip -V
pip 1.5.6 from /path/venv34/lib/python3.4/site-packages (python 3.4)
I have python3 numpy package - python3-numpy_1.12.0-2~pn0_amd64.deb
which I have installed with sudo dpkg -i python3-numpy_1.12.0-2~pn0_amd64.deb
which successfully completed.
Now when I do
python
Python 3.4.2 (default, Feb 7 2019, 06:08:06)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'numpy'
>>>
Any clue what's wrong here?
python3.4 -m pip install numpy==1.12.0-2
ok since my repo is less than 50 i can not add comments, so take this answer as a comment to your question.
I think numpy is installed but not in your virtualenv, make sure your virtualenv is active when you are trying to install any library, you will see virtualenv name in every command line if it is activated.
(venv) C:\Users\seventeen\sprint25>
Try python -m pip install numpy==1.12.0. This should help you.

ImportError: cannot import name 'PrintTable' in Python 3.5.1 by pyenv

I installed pyenv to manage different versions of Python, and use pip install printtable to download and install printtable.
But when I import this module in interactive shell, it doesn't work and shows ImportError.
$ pyenv versions
system
2.7.11
* 3.5.1 (set by /Users/apple/.pyenv/version)
$ pip list
pip (8.0.0)
printtable (1.2)
setuptools (18.2)
$ python
Python 3.5.1 (default, Jan 21 2016, 12:50:43)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import printtable
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/apple/.pyenv/versions/3.5.1/lib/python3.5/site-packages/printtable/__init__.py", line 1, in <module>
from printtable import PrintTable
ImportError: cannot import name 'PrintTable'
How can I manage the modules in pyenv?
PS. I'm following the book Automate the boring stuff step by step. The printtable part is in the end of Chapter 6.
Visit: https://automatetheboringstuff.com/chapter6/
I downloaded printtable with
pip3 install --download /tmp printtable
and inspected the contentents of printtable-1.2.tar.gz. In printtable/printtable.py there is code like
def printTable(self,line_num=0):
....
print self.StrTable
indicating that this package is not python 3 compatible.
It may be possible to install this module by
tar xfv printtable-1.2.tar.gz
cd printtable-1.2/
2to3-3.5 --write printtable/*.py tests/*.py
python3.5 setup.py build
python3.5 setup.py install
You’re not doing anything wrong; the printtable module is simply written in a way that’s entirely incompatible with Python 3.
Since you’re already using pyenv, and according to the output of pyenv versions you already have Python 2.7.11 installed, you can simply install printtable for that Python interpreter:
pyenv shell 2.7.11
pip install printtable

Export libraries from python 2 from python 3

I am working with python 3. My problem is that each time I install a package, it installs it for python2.
For example, I want to use mapnik, so I've just installed it with homebrew, and then it created a folder for python2.7 in mapnik lib.
So it works when I use Python2.7 but not with python3, cause it cannot find the module mapnik.
How can I add it to python 3 ?
python3 path : /Users/gabrielgautron/documents/python3
At the end of mapnik's installation I have the following :
For non-homebrew Python, you need to amend your PYTHONPATH like so:
export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
So I launch this command in terminal, and then :
MacBook-Pro-de-Gabriel-Gautron:local gabrielgautron$ python3
Python 3.2.4 (v3.2.4:1e10bdeabe3d, Apr 6 2013, 11:25:28)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import mapnik
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/mapnik/__init__.py", line 69, in <module>
from _mapnik import *
ImportError: No module named _mapnik
A idea ?
Please :)
Looking at the brew recipe, it depends on the version of python that's accessible when you run brew:
def which_python
"python" + `python -c 'import sys;print(sys.version[:3])'`.strip
end
So given that, you should change your PATH when invoking brew so that you're using python3 as python. For example:
# Figure out the path to python3
PY3DIR=`dirname $(which python3)`
# And /then/ install with brew. That will have it use python3 to get its path
PATH=$PY3DIR:$PATH brew install mapnik

Installing PIL on OS X Lion

I am trying to install PIL on Mac OSX 10.7.4 but after several hours attempt couldn't succeed. All the time I have encountered the same problem provided detail in pastebin link below. Enlighten me!!
Setting
tbc:~ mystic$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
Pastebin
I searched and tried from several sources:
One
Two
Three
Four (Stackoverflow)
Five
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import Image
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named Image
>>> import PIL
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named PIL
>>>
Edited as suggested by jdi but still running through the same problem as shown in above pastebin link
tbc:jpeg-8c mystic$ xcode-select -version
xcode-select version 2003.
tbc:jpeg-8c mystic$ which xcode-select
/usr/bin/xcode-select
tbc:jpeg-8c mystic$ xcode-select -print-path
/Applications/Xcode.app/Contents/Developer
tbc:jpeg-8c mystic$
Further, I gave try with homebrew as suggested by the "the paul"
tbc:cellar mystic$ brew link jpeg
Linking /usr/local/Cellar/jpeg/8d... 3 symlinks created
tbc:cellar mystic$ brew install pil
Error: pil-1.1.7 already installed
tbc:cellar mystic$ brew uninstall pil
Uninstalling /usr/local/Cellar/pil/1.1.7...
tbc:cellar mystic$ brew install pil
==> Downloading http://effbot.org/downloads/Imaging-1.1.7.tar.gz
Already downloaded: /Library/Caches/Homebrew/pil-1.1.7.tar.gz
==> python setup.py build_ext
==> python setup.py install --prefix=/usr/local/Cellar/pil/1.1.7
==> Caveats
This formula installs PIL against whatever Python is first in your path.
This Python needs to have either setuptools or distribute installed or
the build will fail.
Warning: Non-executables were installed to "bin".
Installing non-executables to "bin" is bad practice.
The offending files are:
/usr/local/Cellar/pil/1.1.7/bin/pilfont.py
==> Summary
/usr/local/Cellar/pil/1.1.7: 174 files, 2.0M, built in 15 seconds
tbc:cellar mystic$ python
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pil
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pil
>>> from PIL import Image
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named PIL
Here is the log of .pip
Its complaining about
/Developer/SDKs/MacOSX10.6.sdk/usr/include/stdarg.h:4:25: error: stdarg.h: No such file or directory
but stdarg.h file is there at the same location.
I am not understanding why its trying to locate in MacOSX10.6.sdk as my current version of Lion isMacOSX10.7.sdk
Getting crazy!!!
If you are on Lion, using the newest XCode, then a potential problem for you is that they moved the location of the developer SDKs. Packages that expected them to live in /Developer/ would no longer find them as needed.
Reference this article about specifics:
http://www.agile-workers.com/web/2012/03/qt-qmake-osx_sdk-xcode/
But in summary, what you might need to do is run:
sudo /usr/bin/xcode-select -switch /Applications/Xcode.app/Contents/Developer
to point at the new location.
A manual fix might be to just symlink the SDK from /Applications/Xcode.app/Contents/Developer/... => /Developer/..., but I would first try the xcode-select tool.
Update: Some more suggestions based on your new updates
First off, I can't tell from what you posted whether PIL failed to build because of the SDK or not. If it built successfully, and you are using the standard apple python, then chances are the homebrew python path is not in your own python path.
$ python
>>> import sys
>>> sys.path.append("/usr/local/lib/python2.7/site-packages")
>>> from PIL import Image
If that still errors, then I suppose the issue is still related to missing SDK location.
You could symlink the new location of the SDK to the old one with:
mkdir -p -v /Developer/SDKs
ln -s /Applications/Xcode.app/Contents/Developer/MacOSX10.6.sdk /Developer/SDKs/MacOSX10.6.sdk
Or, you can try explicitly setting the the sdk path when you build pil:
brew remove pil
export SDKROOT=/Applications/Xcode.app/Contents/Developer/MacOSX10.6.sdk
brew install pil
Easy solution:
install Homebrew
brew install pillow
:)
If you have Homebrew installed, this will do the the job:
brew install pillow

Categories

Resources