Python cannot import SymPy: have you installed SymPy? - python-3.x

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.

Related

My Python set-up is too complicated, and I don't understand it. Currently my Python is unusable. Advice would be welcome

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.

Numpy cannot be imported even though it is installed

I am using Linux Mint 19.3 XFCE.
I have installed Numpy through pip3. pip3 was not installed already, and I installed pip3 thorugh apt.
The default version of python3 that came with the OS is 3.6.9. Since I am not supposed to change the default version of Python that comes installed with the OS, I kept that. And I installed a newer version, 3.8.0 with snap.
The command was-
sudo snap install python38
And now, whenever I need to work with the interpreter, I just type python38 into the terminal and get on with it.
I recently installed Numpy with pip3-
pip3 install numpy
and it shows up when I run pip3 freeze
:
It is listed as-
numpy==1.18.1
But when I enter the Python interpreter through typing python38 into my terminal, and type in import numpy, I am shown an error:
import numpy as np
Traceback (most recent call last):
File "", line 1, in
ModuleNotFoundError: No module named 'numpy'
However, when I try the same with Python 3.6.9, it works. Numpy is improted, works just fine. (This time I enter Python interpreter by typing python3)
Now, how do I permanently solve this? That is, being able to import Numpy when working in Python 3.8 in terminal.
This may be the reason because your pip is configured for the default version of python in the machine(Python 3.6.9 in your case). You may change your default python version.
Or
You could run pip and install your python package for specific python version like
python_version -m pip install your_package
eg
python38 -m pip install numpy

Unable to import pyPyrTools in python3

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.

cant import python module after installation

I am trying to use Pexpect for a python script I am writing with python 3. I tried installing the module on commmand prompt with admin privileges by using the pip install command:
pip install Pexpect
Once the module finished installing I opened IDLE and in it and tried importing the module:
import pexpect
which gave the error:
Traceback (most recent call last):
File "", line 1, in
import pexpect
ImportError: No module named 'pexpect'
The problem is python cant see the imported module even after its installation.How can i prevent this from happening?
This might occur if you have multiple versions of Python installed on your machine. Assuming you have Python 2.7 and 3 installed, I am guessing "pip" installed pexpect under the 2.7 libraries. Easiest way around this is to add the path to your Python 2.7 packages to your sys.path.
import sys
sys.path.append('/usr/lib/python2.7/dist-packages')
The path mentioned above tends to change depending on your Python installation. So make sure to validate the path before running your script.
Alternatively, you could use pip3 to install packages for Python 3 directly. Please refer this question for instructions.

python3 - No module named 'html5lib'

I'm running a python3 program that requires html5lib but I receive the error No module named 'html5lib'.
Here are two session of terminal:
sam#pc ~ $ python
Python 2.7.9 (default, Mar 1 2015, 12:57:24)
[GCC 4.9.2] on linux2
>>> import html5lib
>>> html5lib.__file__
'/usr/local/lib/python2.7/dist-packages/html5lib/__init__.pyc'
>>> quit()
sam#pc ~ $ python3
Python 3.4.2 (default, Oct 8 2014, 10:45:20)
[GCC 4.9.1] on linux
>>> import html5lib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'html5lib'
>>>
Where can be the problem?
Seems you have the module only for python 2. Most probably need to install it for python3. Usually use pip3 for that.
pip3 install html5lib
You can check your installed modules using:
pip freeze (or pip3 freeze)
I strongly recommend you to use virtualenv for development. So you can separate the different python versions and libraries/Modules by project.
use:
pip3 install virtualenv
You can then easily create "environments" using (simple version)
virtualenv projectname --python=PYTHON_EXE_TO_USE
This creates a directory projectname. You just switch into that dir and do a
Scripts\activate (on linux/unix: source bin/activte)
And boom. You have an isolated environment with the given python.exe and no installed modules at all. You also have an isolated pip for that project. Really helps a lot.
To end working in that project do a:
Scripts\deactivate (on linux: deactivate)
Thats it.
ONe moer thing ;) You can also do a
pip freeze > requirements.txt
to save all needed dependencies for a project in a file.
Whenever you need to restart from scratch in a new virtualenv you cabn simply do a:
pip install -r requirements.txt
This installs all needed modules for you. Add a -U to get the newest version.

Resources