python3 - No module named 'html5lib' - python-3.x

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.

Related

Python cannot import SymPy: have you installed SymPy?

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.

Imported module was not found?

I am building a web scraper and I am trying to import the 'requests' package but I am getting an error. I am being told the following:
ModuleNotFoundError: No module named 'requests'
Full Error:
(venv) USERs-MacBook-Pro:Scraper user$ /usr/local/opt/python#3.9/bin/python3.9 /Users/user/git/ML/Python/Practice/Scraper/Scraper.py
Traceback (most recent call last):
File "/Users/user/git/ML/Python/Practice/Scraper/Scraper.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
Steps I took when setting up project:
python3 -m venv project_name/venv
source project_name/venv/bin/activate
python3 -m pip install SomePackage
Confirmation package and dependences were installed:
(venv) USERs-MacBook-Pro:Scraper user$ pip list
Package Version
---------- ---------
certifi 2020.12.5
chardet 4.0.0
idna 2.10
pip 20.2.3
requests 2.25.1
setuptools 49.2.1
urllib3 1.26.2
By using the absolute path to system Python like that:
/usr/local/opt/python#3.9/bin/python3.9 /Users/user/git/ML/Python/Practice/Scraper/Scraper.py
you are using system's Python 3.9 and the packages that are installed for that one although you are in a virtual environment.
When creating a virtual environment you are creating a separate environment with the specified python version and all the packages you install with pip are installed to this environment and this python version.
You can better understand that if you run:
which python
inside your virtual environment.
This will show you the python that is used when you run python inside your venv and it's going to be different than
/usr/local/opt/python#3.9/bin/python3.9
So, by having installed requests using pip inside your environment, it is installed in the environments python which is executed when you run just python.
To sum up, to use the packages installed inside your venv with pip you should run your script (after activating the venv) with:
python /Users/user/git/ML/Python/Practice/Scraper/Scraper.py

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

pip3 works with python3.2 but won't work with python3.3

I have created a virtual environment for python3
virtualenv -p /usr/bin/python3 myenv
Everything works ok and pip installs python3 packages. Then I need to upgrade my python3.2 to python3.3. I am using ubuntu 12.04, so I need to do what Luper Rouch suggests here through ppa
I copy /usr/bin/python3.3 into myenv/bin/python3, thus (inside myenv):
python --version
returns Python 3.3.5. But then pip stops working failing with
pip install urllib3
Traceback (most recent call last):
File "mypathtoevn/bin/pip", line 7, in <module>
from pip import main
ImportError: No module named 'pip'
Then I understand that I have to use
pip3.3 install <package_name>
but I can't find any other pip*.* version either globally or inside virtualenv in my system.
How can I get pip3.3 or any other pip version?
Thanks
The ppa you have installed does not contain pip. The python package installer is itself installed with easy_install. On the question you linked to follow the later parts of this answer
Basically, calling
wget http://python-distribute.org/distribute_setup.py
python distribute_setup.py
easy_install pip
should be sufficient. You should use the easy_install command of your Python3.3 install.
Check with
which python
where your python comes from, and search for easy_install in that path. In your case you know where it resides: Your virtualenv.

OpenShift Python3.3 Cartridge trouble upgrading numpy/installing scipy

I have an OpenShift gear with a python3.3 cartridge. On this, I am attempting to install Scipy. I have tried the simplest method of simply putting scipy in the install_requires field of the setup.py file.
This did not work, I get the error:
ImportError: No module named 'numpy.f2py'
Okay so perhaps I need to upgrade my version of numpy. I try the obvious again of changing numpy to numpy==1.8.2 in the install_requires field of the setup command. This appears to install fine, but I get the same error from the installation of scipy.
Okay, so now I try ssh-ing into my openshift gear (rhc ssh app-name). I try the first thing to simply pip install scipy, and I basically get the same error:
$ source ~/python/virtenv/venv/bin/activate
(venv) $ pip install scipy
...
ImportError: No module named 'numpy.f2py'
Cleaning up ...
...
Okay so I try a pip freeze:
(venv) $ pip freeze
...
numpy=1.7.2
...
Similarly:
(venv) $ python
Python 3.3.2 (default, Mar 20 2014, 20:25:51)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy.f2py
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'numpy.f2py'
Okay so let's try forcing the upgrade:
(venv) $ pip install --upgrade -I numpy
.... success ....
(venv) $ pip freeze
...
numpy=1.7.2
Hmm. I try it with and without the -I and with and without numpy==1.8.2. Each with the same result. Numpy really wants to stay at the globally installed version. Now if I look into the lib directory I see:
(venv) $ ls ~/python/virtenv/venv/lib/python3.3/site-packages
bottle-0.12.7-py3.3.egg pip-1.5.6.dist-info
docopt-0.6.2-py3.3.egg __pycache__
easy-install.pth pyparsing-2.0.2-py3.3.egg
Jinja2-2.7.3-py3.3.egg SQLAlchemy-0.9.7-py3.3-linux-x86_64.egg
lazy-1.2-py3.3.egg virtualenv-1.11.6.dist-info
...
Okay so basically every package I have in my install_requires field of the setup.py other than numpy. So now I look in the lib64 directory:
(venv) $ ls ~/python/virtenv/venv/lib64/python3.3/site-packages
numpy numpy-1.8.0-py3.3.egg-info numpy-1.8.1-py3.3.egg-info numpy-1.8.2-py3.3.egg-info
So basically every upgraded version of numpy I have attempted to install (which have reported success). So, how do I get pip freeze to see into this directory? I guess more generally I need the pip echo-system and python to see the installed packages in this directory.
Or alternatively, what am I doing wrong?

Resources