Some of my Python package does not work in OSX console - python

I am new in Python. I have trouble in my OSX.
My Python version
python --version
Python 2.7.8
My ipython version
IPython 2.3.0 -- An enhanced Interactive Python.
My OSX version
Yosemit 10.10.1
I am confused because some of my Python lib work well in console such as when I tried to import urllib but some of my Python libs also does not work in console. But all of my python packages work well in ipython.
When I tried in my console
>>> import numpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named numpy
>>> import urllib
>>> import matplotlib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named matplotlib
>>>
When I tried in my ipython console
In [1]: import numpy
In [2]: import urllib
In [3]: import matplotlib
In [4]:
Result of which python
users-MacBook-Pro:~ user$ which python
python is /opt/local/bin/python
python is /usr/local/bin/python
python is /Library/Frameworks/Python.framework/Versions/2.7/bin/python
python is /usr/local/bin/python
python is /usr/bin/python
python is /usr/local/bin/python
users-MacBook-Pro:~ user$ which -a ipython
ipython is /Library/Frameworks/Python.framework/Versions/2.7/bin/ipython

Try to export python site packages PATH in your console
export PYTHONPATH="$PYTHONPATH:/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages"
Another way, add that path to .bash_profile file in your home directory. For more details read this documentation.

Related

Trying to resolve ModuleNotFoundError: No module named

Trying to install third party modules on my mac using pip and I am getting this error (Using IDLE as the Python shell):
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import pyperclip
ModuleNotFoundError: No module named 'pyperclip'
(Have tried multiple modules and all return the same error)
I have installed the modules using:
pip3 install pyperclip
I can see that it has installed into the directory:
/opt/homebrew/lib/python3.9/site-packages
Ive tried going through several of the posts posted on StackOverflow such as this and this but after trying their steps I cant seem to resolve it.
which -a pip3 gives:
/opt/homebrew/bin/pip3
/Library/Frameworks/Python.framework/Versions/3.9/bin/pip3
/usr/local/bin/pip3
/usr/bin/pip3
which -a python3 gives:
/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
/usr/local/bin/python3
/usr/bin/python3
print(sys.version) gives:
3.9.1 (v3.9.1:1e5d33e9b9, Dec 7 2020, 12:44:01)
[Clang 12.0.0 (clang-1200.0.32.27)]
pip --version gives:
pip 22.1.2 from /opt/homebrew/lib/python3.9/site-packages/pip (python 3.9)
Running /usr/local/bin/python3 test1.py:
Traceback (most recent call last):
File "/Users/dxz/Downloads/test1.py", line 1, in <module>
import pyperclip
ModuleNotFoundError: No module named 'pyperclip'
Thanks
You can run this command for seeing pip3 uses which python:
pip3 --version
But i guess you are not choosing right python to run your .py file
if you run your .py like this:
python3 main.py
You must be sure pip3 installing libraries to 'python3' linked python or you can run your main.py file with one of pip3 linked python
I mean if pip3 --version gives you
/usr/bin/python3
this python path you can run your main.py like this
/usr/bin/python3 main.py
but if you want to use best practice i recommend to you using virtualenv

How to install python packages to embedded python in GDB/ clion2017.1

I'm using CLion 2017.1 and GDB for debugging. GDB compiled with python 2.7 support, but with no modules.
I wish to use matplotlib in order to plot some debug data (using GDB image watch) but there is no matplotlib presence. and no pip to install it.
I've tried to install pip using (gdb) python import get_pip but
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/user/temp/get_pip.py", line 28, in <module>
import tempfile
File "/home/user/clion-2016.3.4/bin/gdb/lib/python2.7/tempfile.py", line 35, in <module>
from random import Random as _Random
File "/home/user/clion-2016.3.4/bin/gdb/lib/python2.7/random.py", line 48, in <module>
from binascii import hexlify as _hexlify
ImportError: No module named binascii
Error while executing Python code.
So I don't know how to bootstrap instullation of additional modules into GDB's embedded python.
Any idea how to do that?
I've tried to install pip using (gdb) python import get_pip but
You need not install python modules from gdb shell. You can install them from command line as usual with your package manager or with pip. Make sure that you installed Python2 version of module. Once the module is installed you can use it in gdb embedded python:
$ gdb -q
(gdb) python
>import matplotlib
>end
(gdb)

Mac reinstall Python

I tried to use the Coursera course downloader from here and it worked for few courses but not all. They have recommended to use Python3.X. So I uninstalled Anaconda (version 2) from my Mac and installed Python3.5.1 from Python's website but still the course downloaders did not work as it was using default Python 2.7.
So I deleted Python from /usr/local/lib and now nothing is working. When I do which python it shows ``usr/local/bin/pythonand bothpythonandpython3` runs.
But when I do pip install coursera it throws error as follows:
Traceback (most recent call last):
File "/usr/local/bin/pip", line 5, in <module>
from pkg_resources import load_entry_point
ImportError: No module named pkg_resources
And when I do sudo easy_install pip, I get the following error.
Traceback (most recent call last):
File "/usr/local/bin/easy_install", line 5, in <module>
from pkg_resources import load_entry_point
ImportError: No module named pkg_resources
try Homebrew http://brew.sh/
brew install python3
curl bootstrap.pypa.io/get-pip.py | python3
Your Mac should have Python 2 and Python 3 installed by default. in the terminal "python" will launch Python2, "python3" will launch Python3. With Anaconda you could have just kept the Python 2 version and created an environment for Python 3, http://conda.pydata.org/docs/using/envs.html#managing-environments.
This is the solution you will most likely need, No module named pkg_resources. Setuptools is causing the issue and you will need to wget ez_setup.py. Reinstalling Anaconda will save you a lot of time and pain.

Python wx via Homebrew

I'm having issues install Python wx on my Mac. Here's what I did:
brew install wxpython
which gave this Caveat:
Caveats
Python modules have been installed and Homebrew's site-packages is not
in your Python sys.path, so you will not be able to import the modules
this formula installed. If you plan to develop with these modules,
please run:
mkdir -p /Users/snowcrash/Library/Python/2.7/lib/python/site-packages
echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")'
>> /Users/snowcrash/Library/Python/2.7/lib/python/site-packages/homebrew.pth
So I ran the 2 commands from the Terminal as suggested:
mkdir -p /Users/snowcrash/Library/Python/2.7/lib/python/site-packages
>> echo 'import site; site.addsitedir("/usr/local/lib/python2.7/site-packages")'
but when I try to import wx I am getting:
>>> import wx
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/wx-3.0-osx_cocoa/wx/__init__.py", line 45, in <module>
from wx._core import *
File "/usr/local/lib/python2.7/site-packages/wx-3.0-osx_cocoa/wx/_core.py", line 4, in <module>
import _core_
ImportError: dlopen(/usr/local/lib/python2.7/site-packages/wx-3.0-osx_cocoa/wx/_core_.so, 2): no suitable image found. Did find:
/usr/local/lib/python2.7/site-packages/wx-3.0-osx_cocoa/wx/_core_.so: mach-o, but wrong architecture
and this doesn't seem to help:
How do I install wxPython on Mac OS X?
Any suggestions?
Turned out I had to do this:
defaults write com.apple.versioner.python Prefer-32-Bit -bool no

virtualenv's python can't find PyQt4

Running OSX (10.8.2) here, replaced their python with Homebrew's. All good:
$ which python
/usr/local/bin/python
Installed pip, virtualenv with brew python. Installed pyqt with brew.
If I
$ python
>>> import PyQt4
, no problems. However, if I
(env)$ python
>>> import PyQt4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named PyQt4
Why can't the virtualenv python environment see my system-wide install of pyqt?
Thanks!

Categories