Search code examples
pythonpippypipython-packaging

Pip-installed module cannot be imported


I uploaded my first PyPi package today. It took me a while to figure everything out, but I did it, and now I'm trying to import it to another project of mine.

Unfortunately I get a ModuleNotFoundError. I'm on Windows 10, running python 3.9.6. I've tried everything I've seen on the internet, deleted the venv and recreated it, reuploaded the package to PyPi, installed it with python -m pip install sqlcontroller and even with ./venv/Scripts/python.exe -m pip install sqlcontroller. I'll try to supply all the info I can below.

The PyPi package: https://pypi.org/project/sqlcontroller/

On Github: https://github.com/RedKnight91/sqlcontroller

(venv) PS C:\Users\mikec\Documents\Python\binance-trading-bot-evaluator> python -m pip install sqlcontroller --no-cache-dir --upgrade 
Requirement already satisfied: sqlcontroller in c:\users\mikec\documents\python\binance-trading-bot-evaluator\venv\lib\site-packages (0.0.2)

import sqlcontroller results in:

Traceback (most recent call last):
  File "c:\Users\mikec\Documents\Python\binance-trading-bot-evaluator\src\main.py", line 2, in <module>
    from sqlcontroller import SqlController
ModuleNotFoundError: No module named 'sqlcontroller'

pip show output

(venv) PS C:\Users\mikec\Documents\Python\binance-trading-bot-evaluator> python -m pip show sqlcontroller --files
Name: sqlcontroller
Version: 0.0.2
Summary: Controller class to handle sqlite3 databases.
Home-page: https://github.com/RedKnight91/sqlcontroller
Author: Michael Cazzarolli
Author-email: [email protected]
License: UNKNOWN
Location: c:\users\mikec\documents\python\binance-trading-bot-evaluator\venv\lib\site-packages
Requires:
Required-by:
Files:
  sqlcontroller-0.0.2.dist-info\INSTALLER
  sqlcontroller-0.0.2.dist-info\LICENSE
  sqlcontroller-0.0.2.dist-info\METADATA
  sqlcontroller-0.0.2.dist-info\RECORD
  sqlcontroller-0.0.2.dist-info\REQUESTED
  sqlcontroller-0.0.2.dist-info\WHEEL
  sqlcontroller-0.0.2.dist-info\top_level.txt
  src\__init__.py
  src\__pycache__\__init__.cpython-39.pyc
  src\__pycache__\sqlcontroller.cpython-39.pyc
  src\sqlcontroller.py
  tests\__init__.py
  tests\__pycache__\__init__.cpython-39.pyc
  tests\__pycache__\conftest.cpython-39.pyc
  tests\__pycache__\test_sqlcontroller.cpython-39.pyc
  tests\conftest.py
  tests\test_sqlcontroller.py

where python gives no output from venv:

(venv) PS C:\Users\mikec\Documents\Python\binance-trading-bot-evaluator> where python
(venv) PS C:\Users\mikec\Documents\Python\binance-trading-bot-evaluator> 

but it does from outside:

C:\Users\mikec>where python
C:\Users\mikec\AppData\Local\Programs\Python\Python39\python.exe

pip freeze

(venv) PS C:\Users\mikec\Documents\Python\binance-trading-bot-evaluator> pip freeze
sqlcontroller==0.0.2

Solution

  • You have a wrong file structure

    This is an example how my package looks

    (venv) PS D:\Python\thebot> pip show bdbf --files
    Name: bdbf
    Author: Bertik23
    Author-email: [email protected]
    License: UNKNOWN
    Location: d:\python\thebot\venv\lib\site-packages
    Requires: discord.py
    Required-by:
    Files:
      bdbf-1.1.2.dist-info\INSTALLER
      bdbf-1.1.2.dist-info\LICENSE
      bdbf-1.1.2.dist-info\METADATA
      bdbf-1.1.2.dist-info\top_level.txt
      bdbf\__init__.py
      bdbf\__pycache__\__init__.cpython-39.pyc
      bdbf\__pycache__\bdbf.cpython-39.pyc
      bdbf\__pycache__\exceptions.cpython-39.pyc
      bdbf\__pycache__\functions.cpython-39.pyc
      bdbf\__pycache__\main.cpython-39.pyc
      bdbf\bdbf.py
      bdbf\exceptions.py
      bdbf\functions.py
      bdbf\main.py
    

    As you can see, all of the .py filles are in the bdbf folder, but yours are in src, that is than the name you have to import, so if you do import src you probably would import your package