Search code examples
python-3.xfastapiuvicornhttpx

ModuleNotFoundError: No module named 'httpx'


Getting above error have installing the correct package

Python --version

Python 3.6.9

Install command

pip3 install httpx

pip3 list

anyio (3.6.1)
async-generator (1.10)
Brotli (1.0.9)
certifi (2022.6.15)
charset-normalizer (2.1.0)
contextvars (2.4)
dataclasses (0.8)
dnspython (2.2.1)
email-validator (1.2.1)
h11 (0.12.0)
httpcore (0.14.7)
httpx (0.22.0)
idna (3.3)
immutables (0.18)
MarkupSafe (2.0.1)
pip (9.0.1)
pkg-resources (0.0.0)
pydantic (1.9.1)
python-dateutil (2.8.2)
rfc3986 (1.5.0)
setuptools (39.0.1)
six (1.16.0)
sniffio (1.2.0)
typing-extensions (4.1.1)
validator (0.7.1)

In virtual environment interactive shell the package is also working

    (env) PEOPLE\saurabhkamble@lp7948:/var/www/vip_select_shaadi_api$ uvicorn main:app --reload
    INFO:     Will watch for changes in these directories: ['/var/www/vip_select_shaadi_api']
    INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
    INFO:     Started reloader process [15073] using statreload
    None
    Process SpawnProcess-1:
    Traceback (most recent call last):
      File "/usr/lib/python3.6/multiprocessing/process.py", line 258, in _bootstrap
        self.run()
      File "/usr/lib/python3.6/multiprocessing/process.py", line 93, in run
        self._target(*self._args, **self._kwargs)
      File "/home/saurabhkamble/.local/lib/python3.6/site-packages/uvicorn/subprocess.py", line 76, in subprocess_started
        target(sockets=sockets)
      File "/home/saurabhkamble/.local/lib/python3.6/site-packages/uvicorn/server.py", line 69, in run
        return asyncio.get_event_loop().run_until_complete(self.serve(sockets=sockets))
      File "/usr/lib/python3.6/asyncio/base_events.py", line 484, in run_until_complete
        return future.result()
      File "/home/saurabhkamble/.local/lib/python3.6/site-packages/uvicorn/server.py", line 76, in serve
        config.load()
      File "/home/saurabhkamble/.local/lib/python3.6/site-packages/uvicorn/config.py", line 456, in load
        self.loaded_app = import_from_string(self.app)
      File "/home/saurabhkamble/.local/lib/python3.6/site-packages/uvicorn/importer.py", line 24, in import_from_string
        raise exc from None
      File "/home/saurabhkamble/.local/lib/python3.6/site-packages/uvicorn/importer.py", line 21, in import_from_string
        module = importlib.import_module(module_str)
      File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
      File "<frozen importlib._bootstrap>", line 994, in _gcd_import
      File "<frozen importlib._bootstrap>", line 971, in _find_and_load
      File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
      File "<frozen importlib._bootstrap_external>", line 678, in exec_module
      File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
      File "./main.py", line 3, in <module>
        from routes.member import routes_member
      File "./routes/member.py", line 5, in <module>
        from api import  universities
      File "./api/universities.py", line 3, in <module>
        import httpx
    ModuleNotFoundError: No module named 'httpx'
    INFO:     Stopping reloader process [15073]

enter image description here

Error when running Fastapi on local

(env) PEOPLE\saurabhkamble@lp7948:/var/www/vip_select_shaadi_api$ python3
Python 3.6.9 (default, Mar 15 2022, 13:55:28) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import httpx
>>> r = httpx.get('https://www.example.org/')
^[[Ar
<Response [200 OK]>

error facing while running fastapi application


Solution

  • Your uvicorn seems to be outside your virtualenv (the use of which being evident from (env) in your prompts), in ~/.local (your user site-packages directory). Remove it, then install and use an uvicorn within your env.

    1. Without env activated: pip uninstall --user -y uvicorn
    2. With your env activated: pip install uvicorn

    You might also want to check for other packages that shouldn't have been installed outside a venv: pip list --user and similarly uninstall them to avoid further conflicts.

    You can also set PIP_REQUIRE_VIRTUALENV=false in your environment variable settings to prevent you from accidentally installing packages outside a virtualenv in the future.