I have installed pyPyrTools from here using:
pip install pyPyrTools
It showed a success but when importing this package, gives me an error as:
Traceback (most recent call last):
File "stdin", line 1, in "module"
ModuleNotFoundError: No module named 'pyPyrTools'
How do I use this Package?
Whats wrong?
The way to debug such issues is first to determine which python and which pip you are using. Then, secondly, see where you installed the module and where Python is looking to find the mismatch.
So, if you use the command python to start Python, you would run this:
type python
If you use the command python3 to start Python, you would run this:
type python3
Now run the same command for pip or pip3 according to which you use:
type pip # or "type pip3"
Now see what versions of python and pip you are running:
python -V # or "python3 -V" if you use "python3"
pip -V # or "pip3 -V" if you use "pip3"
You should now know which versions of the tools you are using.
Now see where pip (or pip3 if you use that) installed your pyPyrTools:
pip show pyPyrTools # or "pip3 show pyPyrTools" if you use "pip3"
Sample Output
Name: pyPyrTools
Version: 0.2.3
Summary: Python tools for multi-scale image processing, including Laplacian pyramids, Wavelets, and Steerable Pyramids
Home-page: https://github.com/LabForComputationalVision/pyPyrTools
Author: Eero Simoncelli, Rob Young, and William F. Broderick
Author-email: eero.simoncelli#nyu.edu
License: MIT
Location: /usr/local/lib/python3.7/site-packages
Requires: Pillow, numpy, matplotlib, scipy
Required-by:
Especially important is the Location: field on 3rd last line.
Now check where your Python is actually looking for modules:
python3 -c "import sys; print(sys.path)" # or "python -c ..." if you use "python" rather than "python3"
Sample Output
['', '/Users/mark/StackOverflow', '/Users/mark/OpenCV/lib/python3.7/site-packages', '/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', '/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', '/Users/mark/Library/Python/3.7/lib/python/site-packages', '/usr/local/lib/python3.7/site-packages']
So, the Location: from the previous part of my answer must be listed here for Python to find pyPyrTools. If it isn't, you either installed using a pip that didn't match your python, or you didn't configure your PYTHONPATH correctly.
If you run the above commands, please click edit under your question and paste the output in there - NOT in a comment where it is hard to format and read.
Related
I'm trying to use pip to install a package. I try to run pip install from the Python shell, but I get a SyntaxError. Why do I get this error? How do I use pip to install the package?
>>> pip install selenium
^
SyntaxError: invalid syntax
pip is run from the command line, not the Python interpreter. It is a program that installs modules, so you can use them from Python. Once you have installed the module, then you can open the Python shell and do import selenium.
The Python shell is not a command line, it is an interactive interpreter. You type Python code into it, not commands.
Use the command line, not the Python shell (DOS, PowerShell in Windows).
C:\Program Files\Python2.7\Scripts> pip install XYZ
If you installed Python into your PATH using the latest installers, you don't need to be in that folder to run pip
Terminal in Mac or Linux
$ pip install XYZ
As #sinoroc suggested correct way of installing a package via pip is using separate process since pip may cause closing a thread or may require a restart of interpreter to load new installed package so this is the right way of using the API: subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'SomeProject']) but since Python allows to access internal API and you know what you're using the API for you may want to use internal API anyway eg. if you're building own GUI package manager with alternative resourcess like https://www.lfd.uci.edu/~gohlke/pythonlibs/
Following soulution is OUT OF DATE, instead of downvoting suggest updates. see https://github.com/pypa/pip/issues/7498 for reference.
UPDATE: Since pip version 10.x there is no more get_installed_distributions() or main method under import pip instead use import pip._internal as pip.
UPDATE ca. v.18 get_installed_distributions() has been removed. Instead you may use generator freeze like this:
from pip._internal.operations.freeze import freeze
print([package for package in freeze()])
# eg output ['pip==19.0.3']
If you want to use pip inside the Python interpreter, try this:
import pip
package_names=['selenium', 'requests'] #packages to install
pip.main(['install'] + package_names + ['--upgrade'])
# --upgrade to install or update existing packages
If you need to update every installed package, use following:
import pip
for i in pip.get_installed_distributions():
pip.main(['install', i.key, '--upgrade'])
If you want to stop installing other packages if any installation fails, use it in one single pip.main([]) call:
import pip
package_names = [i.key for i in pip.get_installed_distributions()]
pip.main(['install'] + package_names + ['--upgrade'])
Note: When you install from list in file with -r / --requirement parameter you do NOT need open() function.
pip.main(['install', '-r', 'filename'])
Warning: Some parameters as simple --help may cause python interpreter to stop.
Curiosity: By using pip.exe you actually use python interpreter and pip module anyway. If you unpack pip.exe or pip3.exe regardless it's python 2.x or 3.x, inside is the SAME single file __main__.py:
# -*- coding: utf-8 -*-
import re
import sys
from pip import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(main())
To run pip in Python 3.x, just follow the instructions on Python's page: Installing Python Modules.
python -m pip install SomePackage
Note that this is run from the command line and not the python shell (the reason for syntax error in the original question).
I installed python and when I run pip command it used to throw me an error like shown in pic below.
Make Sure pip path is added in environmental variables. For me, the python and pip installation path is::
Python: C:\Users\fhhz\AppData\Local\Programs\Python\Python38\
pip: C:\Users\fhhz\AppData\Local\Programs\Python\Python38\Scripts
Both these paths were added to path in environmental variables.
Now Open a new cmd window and type pip, you should be seeing a screen as below.
Now type pip install <<package-name>>. Here I'm installing package spyder so my command line statement will be as pip install spyder and here goes my running screen..
and I hope we are done with this!!
you need to type it in cmd not in the IDLE. becuse IDLE is not an command prompt if you want to install something from IDLE type this
>>>from pip.__main__ import _main as main
>>>main(#args splitted by space in list example:['install', 'requests'])
this is calling pip like pip <commands> in terminal. The commands will be seperated by spaces that you are doing there to.
If you are doing it from command line,
try -
python -m pip install selenium
or (for Python3 and above)
python3 -m pip install selenium
I'm using Octave on OS X and I wanted to use the symbolic package.
I had to install the package running pkg install -forge symbolic on Octave, but then I also had to install sympy. I installed both mpmath and sympy running on my terminal two commands: pip install sympy and pip install mpmath.
At this point I tried to use the package launching this small script
pkg load symbolic
syms t
but it gave me this error
Symbolic pkg v2.9.0: Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'sympy'
error: Python cannot import SymPy: have you installed SymPy?
Try "sympref diagnose" for more information.
error: called from
assert_have_python_and_sympy at line 123 column 7
python_ipc_popen2 at line 79 column 5
python_ipc_driver at line 62 column 15
pycall_sympy__ at line 163 column 11
valid_sym_assumptions at line 38 column 10
assumptions at line 82 column 7
syms at line 97 column 13
Then I run the command sympref diagnose as suggested, whit this output
Symbolic package diagnostics
============================
Python and SymPy are needed for most features of the Symbolic package.
The Python interpreter is currently: "python3".
Computers may have more than one Python interpreter installed. If you
need to, you can select a different one using the PYTHON environment
variable (see "help sympref"). For example, to use Python 2, try
setenv PYTHON python2
sympref reset
Attempting to run python3 -c "print(\"Python says hello\")"
status = 0
output = Python says hello
Good, Python ran correctly.
Python version
--------------
Let's check what version of Python we are calling...
Attempting to run python3 -c "import sys; print(sys.version)"
status = 0
output = 3.9.4 (default, Apr 14 2021, 21:04:05)
[Clang 11.0.0 (clang-1100.0.33.17)]
SymPy Python Library
--------------------
SymPy is a Python library used by Symbolic for almost all features.
Attempting to run python3 -c "import sympy; print(sympy.__version__)"
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'sympy'
status = 1
output =
Unfortunately status was non-zero: probably Python cannot import sympy.
* Is there an error message above?
* Do you have SymPy installed? If not, please try to install it and
try again.
* If you do have SymPy installed, maybe it's installed for a different
Python interpreter than the one we found? Please try "setenv" as
described above to change your python interpreter.
Python3 is the right interpreter, but the version is not. If i run the very same code import sys; print(sys.version) I don't get the output Octave provides me
output = 3.9.4 (default, Apr 14 2021, 21:04:05)
[Clang 11.0.0 (clang-1100.0.33.17)]
but I get
3.10.4 (v3.10.4:9d38120e33, Mar 23 2022, 17:29:05) [Clang 13.0.0 (clang-1300.0.29.30)]
May this be the problem?
I also tried to:
uninstall and install again both sympy and mpmath
install both of them using pip3 rather than pip
move mpmath and sympy in the same folder, which is /Users/jacopo/Library/Python/3.10/lib/python/site-packages
downgrade sympy package with the command pip install --user sympy==1.5.1
checked if both of them are installed running pip show <pkg_name>, and they are
restart Octave, Python and the Terminal multiple times
Output of which -a python python3 pip pip3:
/usr/bin/python
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3
/usr/local/bin/python3
/usr/bin/python3
/Library/Frameworks/Python.framework/Versions/3.10/bin/pip
/Library/Frameworks/Python.framework/Versions/3.10/bin/pip3
/usr/local/bin/pip3
/usr/bin/pip3
If you're trying to install sympy globally like I was, try the below instead and see if it makes a difference (it did for me); I'm not sure why it makes any difference, but I suspect it's due to macOS-specific idiosyncrasies. Basically, instead of using pip3, try:
python3 -m pip install sympy
or
sudo python3 -m pip install sympy
After this, the Octave error went away for me. (Also, I'm using macOS Monterey.)
Credit goes to this post: https://stackoverflow.com/a/58224907
You have multiple versions of Python installed, in your shell, python3 is a different interpreter than what Octave finds when it does python3. If you install a package through the shell, it will not be available to the Python interpreter that Octave finds.
You can tell Octave which Python interpreter to use. For example, in your shell, do
which python3
which might say
/usr/local/bin/python3
This is the Python interpreter for which you installed the package.
Then you’d do
export PYTHON /usr/local/bin/python3
octave
to start Octave.
Alternatively, from within Octave, as indicated in the diagnostic message added to the question above,
setenv PYTHON /usr/local/bin/python3
sympref reset
Reference: Octave docs for sympref.
Is there a simple test from the command line (zsh Catalina / bash Ubuntu) that verify installation of a python module?
For example: if pip3 install reportlab is executed, does execution of from the python prompt:
'>>> import reportlab
positively confirm that module is successfully installed? What are the limits (false positives) of this test?
pip show followed by the name of the package.
For example:
pip show pyyaml
Will output something like this:
Name: PyYAML
Version: 5.3.1
Summary: YAML parser and emitter for Python
Home-page: https://github.com/yaml/pyyaml
Author: Kirill Simonov
Author-email: xi#resolvent.net
License: MIT
Location: d:\_venvs\python36\test-geo\lib\site-packages
Requires:
Required-by: yamllint
You can also use pip list and pip freeze to display a list of installed packages.
I would like to preface with the fact that I am a coding newbie and all this is very new to me. In fact, this is my first stack overflow question.
Anyways I am learning python and am trying to install my first third party module called “pyperclip” in Terminal, but I don't think it installed correctly. I am very new with Terminal as well.
For reference, I am following the youtube guide "Automate the Boring Stuff with Python"
https://www.youtube.com/watch?v=xJLj6fWfw6k
I am on Lesson 8 and I am at minute 3:43 in the video if you want to follow along.
Here are some of my system details:
Mac OS X
Python version 3.8.3
I ran the following into Terminal:
sudo pip3 install pyperclip
Then I received the following message in Terminal after installing pyperclip
The directory or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo’s -H flag.
I then tried to install again using the following:
sudo -H pip3 install pyperclip
Then I got this message:
Requirement already satisfied: pyperclip in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (1.8.0)
I tried installing one last time using:
pip3 install pyperclip
I got the same message again:
Requirement already satisfied: pyperclip in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (1.8.0)
This is also the error I get when I try importing pyperclip in my interactive shell:
>>> import pyperclip
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
ModuleNotFoundError: No module named ‘pyperclip’
>>>
Can anyone help me please? I feel like a fish out of the water with all this stuff. Especially when it comes to Terminal. Any help would be greatly appreciated.
I wanted to discuss a few things that might help you on your way.
We typically do not use sudo in conjunction with pip to install Python packages. This installs the package at a system level and could conflict with packages that your system currently has installed or may install packages to a Python installation which is different than the one you are using from the terminal. Instead, we typically use pip install --user rather than sudo pip install.
See this question and answer for more: sudo pip install VS pip install --user.
If you are ever unsure whether a package has been installed properly, check using
pip3 list
We also need to make sure that the Python interpreter you are using is the same as where pip is installing. Since you are using pip3 to install, you should be using python3 at the terminal to enter the interactive shell. You can also verify that you are using the right Python interpreter by typing in the following command into the terminal:
which python3
Then, make sure that the output matches with the /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/ path that was reported by pip3.
If you are using a different interactive shell such as ipython, you need to make sure that you install ipython in the same manner using
pip3 install --user ipython
and executing it using
ipython3
Try repeating your steps using this new information to see if it helps. Let me know if you have any more questions or need some more help.
Here is a sequence of my commands, and my system's response. This shows that I do not know how to access numpy with my current (very confusing) Python setup. It was recently working, but then I changed something, but can't remember what.
I run MacOs 10.14.6 on a Macbook Pro.
dbae$ which $SHELL
/bin/bash
dbae$ bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.
dbae$ echo $PATH
/Users/dbae/bin:/opt/local/bin:/opt/local/sbin:/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/bin:/usr/local/texlive/2018/bin/universal-darwin:/usr/local/texlive/2018/bin/x86_64-darwin:/Library/TeX/texbin:/opt/X11/bin:/Users/dbae/Library/Python/3.7/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:
dbae$ python run.py
Traceback (most recent call last):
File "run.py", line 9, in <module>
import numpy
ModuleNotFoundError: No module named 'numpy'
dbae$ which python
/opt/local/bin/python
dbae$ /opt/local/bin/python --version
Python 3.8.3
dbae$ which pip
/opt/local/bin/pip
dbae$ /opt/local/bin/pip --version
pip 19.3.1 from /opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pip (python 3.6)
dbae$ pip install --upgrade pip
Collecting pip
Using cached https://files.pythonhosted.org/packages/43/84/23ed6a1796480a6f1a2d38f2802901d078266bda38388954d01d3f2e821d/pip-20.1.1-py2.py3-none-any.whl
Installing collected packages: pip
Found existing installation: pip 19.3.1
Uninstalling pip-19.3.1:
ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/opt/local/Library/Frameworks/Python.framework/Versions/3.6/bin/pip'
Consider using the `--user` option or check the permissions.
dbae$ pip install numpy
Requirement already satisfied: numpy in /opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (1.17.4)
Actually, you kind of screwed your pip setup. Start with the beginning :
$ python -m pip list
What is returned by this command ?
I would suggest you start using virtual environment (venv). It is much easier to handle module dependency issues in the future.
$ python -m venv /path/to/env
$ source /path/to/env/bin/activate # Activates your environment
$ python -m pip install numpy
$ python run.py
Doing so will gather all your dependencies in /path/to/env without messing up with your global setup.