Imported module was not found? - python-3.x

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

Related

Import "ruamel.yaml" could not be resolved

I am just a beginner in python.
I have a requirement of converting a JSON Object into a YAML file and trying to import ruamel.yaml.
I did install the command pip3 install ruamel.yaml and my python list package shows that it's available.
site-packages % pip3 list
Package Version
------------------ ---------
certifi 2021.10.8
charset-normalizer 2.0.11
idna 3.3
pip 22.0.3
pytz 2021.3
PyYAML 6.0
requests 2.27.1
ruamel.yaml 0.17.21
ruamel.yaml.clib 0.2.6
setuptools 60.9.3
urllib3 1.26.8
Whereas when I am trying to import and use it in the python script it is giving me below error.
Import "ruamel.yaml" could not be resolved
Could someone please suggest how to resolve this.?
When Python cannot find a module you get an error looking like:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named ruamel.yaml
This might be because you are using some non-standard installation/execution environment, or because you try to run this from an editor. Some of those don't understand the concept of namespaces in Python.
It could also be that pip3 is not installing for the python that you are using to run the program. That is easy to check by installing some other package using pip3 and trying to import that.
In either case make sure you test your program running with the python that corresponds to pip3 ( using type pip3 or which pip3 resp type python ), or install using:
python -m pip install ruamel.yaml
and then run the program with python your_prog.py

How to install fenics in Ubuntu using PIP for a particular python environment?

I want to install fenics in Ubuntu 20.
First I made a python environment using:
sudo apt install python3-venv
Then inside the folder I want to make an environment I open a terminal and use:
python3 -m venv myproject
myproject is the name of the environment I made.
I then activate my environment:
source myproject/bin/activate
To install fenics for this particular environment while I activated the environment, I use:
pip install fenics
I verify the installation using pip list which returns:
Package Version
-------------- --------------
fenics 2019.1.0
fenics-dijitso 2019.1.0
fenics-ffc 2019.1.0.post0
fenics-fiat 2019.1.0
fenics-ufl 2019.1.0
mpmath 1.1.0
numpy 1.19.4
pip 20.0.2
pkg-resources 0.0.0
setuptools 44.0.0
sympy 1.7.1
I try to import fenics using:
python -c "import fenics"
But I get the error below stating there is not fenics module:
raceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'fenics'
What is the problem? Hopefully, after successful installation of fenics, I can install Spyder and other python packages and will be able to use fenics inside spyder.
Edit
I want to install fenics in a python virtual environment.
The issue here is that fenics is just a meta-package and it does not contain any library that you can use in your Python code.
Quoting from the README for the fenics project :
This package contains a single file, setup.py, that allows all of the FEniCS Python components to be installed from PyPI using pip:
pip3 install fenics
Actual use of the library is done via
import ffc
All different components are under this package. For example, fenics-fiat is available as ffc.fiatinterface.
Fenics library of python
If you have installed pip than you have to use pip install fenics. If you have installed pip3 than `pip3 install fenics
I have already installed both.
sudo apt update
sudo apt install python3-pip
Install pip for Python 2 with:
sudo apt install python-pip
After doing pip3 install fenics,write python3 -c import ffc to import it. If this works, it has been imported. It is called ffc, not fenics

No module named 'request' even after it is already there while running "Facebook AI Performance Evaluation Platform"

I searched a lot and tried many of the SQ solutions but nothing helped me here... can someone check what exactly I messed up with?
My Python3 is installed in the directory here /usr/local/Cellar/python/3.7.0/
In my .bashrc I have added
export PYTHONPATH=$PYTHONPATH:/Users/swapnil.kotwal/Library/Python/3.7/lib/python/site-packages
Also, .bash_profile have
source ~/.bashrc
alias python=python3
export PATH="/usr/local/bin:/usr/local/opt/node#10/bin:$PATH"
My Below command also works fine
$ python -c "import requests; print (requests.__version__)"
2.20.0
And other things are as below
SwapnilsMacBook:FAI-PEP swapnil.kotwal$ /usr/bin/python --version
Python 2.7.10
SwapnilsMacBook:FAI-PEP swapnil.kotwal$ which python
/usr/bin/python
SwapnilsMacBook:FAI-PEP swapnil.kotwal$ whereis python
/usr/bin/python
SwapnilsMacBook:FAI-PEP swapnil.kotwal$ python3 --version
Python 3.7.0
SwapnilsMacBook:FAI-PEP swapnil.kotwal$ which pip3
/usr/local/bin/pip3
SwapnilsMacBook:FAI-PEP swapnil.kotwal$ vi benchmarking/run_bench.py
SwapnilsMacBook:FAI-PEP swapnil.kotwal$ benchmarking/run_bench.py -b specifications/models/caffe2/shufflenet/shufflenet.json
Traceback (most recent call last):
File "benchmarking/run_bench.py", line 18, in <module>
from utils.utilities import getPythonInterpreter, getString
File "/Users/swapnil.kotwal/Swapnil/FAI-PEP/benchmarking/utils/utilities.py", line 16, in <module>
import requests
ImportError: No module named requests
SwapnilsMacBook:FAI-PEP swapnil.kotwal$
SwapnilsMacBook:FAI-PEP swapnil.kotwal$ pip3 install six
Collecting six
Using cached https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
Installing collected packages: six
Successfully installed six-1.11.0
SwapnilsMacBook:FAI-PEP swapnil.kotwal$ pip3 install requests
Collecting requests
Using cached https://files.pythonhosted.org/packages/f1/ca/10332a30cb25b627192b4ea272c351bce3ca1091e541245cccbace6051d8/requests-2.20.0-py2.py3-none-any.whl
Collecting chardet<3.1.0,>=3.0.2 (from requests)
Using cached https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl
Collecting certifi>=2017.4.17 (from requests)
Using cached https://files.pythonhosted.org/packages/56/9d/1d02dd80bc4cd955f98980f28c5ee2200e1209292d5f9e9cc8d030d18655/certifi-2018.10.15-py2.py3-none-any.whl
Collecting urllib3<1.25,>=1.21.1 (from requests)
Using cached https://files.pythonhosted.org/packages/8c/4b/5cbc4cb46095f369117dcb751821e1bef9dd86a07c968d8757e9204c324c/urllib3-1.24-py2.py3-none-any.whl
Collecting idna<2.8,>=2.5 (from requests)
Using cached https://files.pythonhosted.org/packages/4b/2a/0276479a4b3caeb8a8c1af2f8e4355746a97fab05a372e4a2c6a6b876165/idna-2.7-py2.py3-none-any.whl
Installing collected packages: chardet, certifi, urllib3, idna, requests
Successfully installed certifi-2018.10.15 chardet-3.0.4 idna-2.7 requests-2.20.0 urllib3-1.24
pip list
SwapnilsMacBook:FAI-PEP swapnil.kotwal$ pip list
Package Version
---------------- ----------
certifi 2018.10.15
chardet 3.0.4
idna 2.7
pip 18.1
pipenv 2018.10.13
requests 2.20.0
setuptools 40.4.3
six 1.11.0
urllib3 1.24
virtualenv 16.1.0
virtualenv-clone 0.4.0
wheel 0.32.0
SwapnilsMacBook:FAI-PEP swapnil.kotwal$
But, when I ran FAI-PEP
https://github.com/facebook/FAI-PEP
using command benchmarking/run_bench.py -b specifications/models/caffe2/shufflenet/shufflenet.json
I got error as below
$ benchmarking/run_bench.py -b specifications/models/caffe2/shufflenet/shufflenet.json
Traceback (most recent call last):
File "benchmarking/run_bench.py", line 18, in <module>
from utils.utilities import getPythonInterpreter, getString
File "/Users/swapnil.kotwal/Swapnil/FAI-PEP/benchmarking/utils/utilities.py", line 16, in <module>
import requests
ImportError: No module named requests
Please check where requests gets installed. Then check whether python is searching for the library in this directory.
In order to see where requests is installed, you can use the following pip command
pip list
compare How do I find the location of my Python site-packages directory? . In case the Location column is empty, the library was installed to the default directory.
And to see the directories in which python is searching for libraries you can use
print(__import__('sys').path)
In case requests is installed in a directory python is not looking in, I suggest reading these question: Permanently add a directory to PYTHONPATH , Install a Python package into a different directory using pip?
Also check that your python script is not executed in a virtual environment (virtualenv) where the package is not installed/available https://docs.python-guide.org/dev/virtualenvs/.

No module named 'info' on fresh Python 3 installation

I did a fresh python3 installation on OSX via homebrew:
brew install python3
Then I created a virtual environment for my project and installed scipy and scikits.samplerate:
virtualenv -p /usr/local/bin/python3 pythen_env
pip install scipy
pip install scikits.samplerate
However, when I try to import a function from scikits.samplerate, I get the following error:
>>> from scikits.samplerate import resample
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/my/project/path/pythen_env/lib/python3.6/site-packages/scikits/samplerate/__init__.py", line 4, in <module>
from info import __doc__
ModuleNotFoundError: No module named 'info'
Info happens to be the first module from the package itself that is imported in __init__.py.
Strangely, the module info.py exists in /my/project/path/pythen_env/lib/python3.6/site-packages/scikits/samplerate/:
ls /my/project/path/pythen_env/lib/python3.6/site-packages/scikits/samplerate/
__init__.py setup.py tests __pycache__
info.py setuphelp.py version.py
The error also happens when I try the same without virtualenv, as well as for other packages. How could I start to debug this issue?
The problem seems to be that the package scikits.samplerate does not support Python 3.X (see issue). However, there is a fork which supports Python 3.X. You can install it via
$ pip install git+https://github.com/gregorias/samplerate.git
As always: people can make anything they like in repositories. I did not check which changes gregorias made.
the git version does support py3
https://github.com/cournape/samplerate
(merged the PR from #gregorias)
I should find the time and procedure to update pypi too...

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.

Resources