Ira Kloostra Nathan
Ira Kloostra Nathan

Reputation: 148

Python directory for pip package not working

I have made a pip package but I can't import stuff from the file 'src'? I think it might be something with init.py but im not sure why.

Here is my directory:

./
├── dist
│   └── robloxapi-0.9.tar.gz
├── LICENSE.txt
├── MANIFEST
├── README.md
├── robloxapi
│   ├── client.py
│   ├── __init__.py
│   ├── __main__.py
│   └── src
│       ├── __pycache__
│       │   ├── Auth.cpython-37.pyc
│       │   ├── __init__.cpython-37.pyc
│       │   ├── request.cpython-37.pyc
│       │   ├── User.cpython-37.pyc
│       │   └── xcsrf.cpython-37.pyc
│       ├── request.py
│       ├── User.py
│       └── xcsrf.py
├── setup.cfg
└── setup.py

here is what I am importing in my main file (client.py)

import requests
from .src.User import User

When I try import .src.User it returnes an error.

witch is:

Traceback (most recent call last):
  File "index.py", line 1, in <module>
    import robloxapi
  File "/home/ira/.local/lib/python3.7/site-packages/robloxapi/__init__.py", line 3, in <module>
    from .client import client
  File "/home/ira/.local/lib/python3.7/site-packages/robloxapi/client.py", line 2, in <module>
    from .src.User import User
ModuleNotFoundError: No module named 'robloxapi.src'

Why am I unable to import this?

Thanks, Ira.

Edit: Setup.py: https://hastebin.com/tonalezeva.coffeescript setup.cfg: https://hastebin.com/ovehukociz.ini

Upvotes: 1

Views: 195

Answers (1)

ch0wner
ch0wner

Reputation: 349

The error that was raised ModuleNotFoundError indicates that python is trying to import robloxapi.src as a module.

According to your project structure, the directory should be a python package, but it is not because it is missing an __init__.py file.

Creating a file at robloxapi/src/__init__.py (which can also be left empty) should resolve this issue.

Quoting the official python docs about modules and packages

The __init__.py files are required to make Python treat directories containing the file as packages. This prevents directories with a common name, such as string, unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.

Edit

After I've looked at your setup.py file I believe that the problem is that you do not specify the sub-packages in your project in the packages list passed to the setup function.

I also noticed you are using distutils, I highly recommend you install and use the setuptools package which is a highly popular extension to the built in distutils package.

One of setuptools great features is the find_packages function which saves the burden of managing the packages to be distributed manually

Heres a reference to the setuptools docs:

https://setuptools.readthedocs.io/en/latest/setuptools.html#id10

Upvotes: 1

Related Questions