Zip
Zip

Reputation: 71

Cannot import a setup.py module while inside a venv

I'm trying to test a package that I hope to eventually register on PyPI. Unfortunately, I can't seem to install it correctly on my own machine, or at least, I cannot use the module via import. I keep getting an ImportError citing unknown location. Ultimately I am looking for a solution that will work not just for me, but for people pip installing my package remotely, so something like "adding x to the path" isn't going to work unless the problem will only happen on my machine.

Tree of the repo. Both init.py are blank and so is requirements.txt; the thing I actually want to package is coolpackage.py

    .
    ├──  __init__.py
    ├── MANIFEST.in
    ├── Makefile
    ├── README.md
    ├── examples
    │   ├── example_1.py
    │   ├── example_2.py
    │   └── example_3.py
    ├── requirements-dev.txt
    ├── requirements.txt
    ├── setup.py
    ├── src
    │   ├──  __init__.py
    │   ├── coolpackage.py
    │   └── coolpackage.pyi
    └── example4.py

setup.py:

from setuptools import setup, find_packages

setup(
    name='coolpackage',
    version='0.0.2',
    packages=['coolpackage'],
    package_dir={'coolpackage': 'src'},
    include_package_data=True,
    package_data={"coolpackage": ["*.md", "*.pyi"]},
    zip_safe=False,
    platforms=["MacOS X", "Posix"],
    classifiers=[
        "Programming Language :: Python :: 3.7"
    ]
)

requirements-dev.txt:

flake8
wheel
setuptools

examples/example_3.py:

import pkg_resources
installed_packages = pkg_resources.working_set
installed_packages_list = sorted(["%s==%s" % (i.key, i.version)
   for i in installed_packages])
print(installed_packages_list)

from coolpackage import AwesomeEntry, CoolAwesome

Process:

  1. Make and activate a fresh venv in one folder above this repo
  2. cd into this repo
  3. pip3 install -r requirements-dev.txt
  4. python3 setup.py bdist_wheel
  5. pip3 install dist/coolpackage-0.0.2-py3-none-any.whl
  6. flake8 --ignore E501,E231,E128 src/coolpackage.py
  7. python3 examples/example_3.py

The last command, python3 examples/example_3.py, prints this, indicating that coolpackage exists and is installed, but I don't see why it can't be imported.

['coolpackage==0.0.2', 'flake8==4.0.1', 'importlib-metadata==4.2.0', 'mccabe==0.6.1', 'pip==20.1.1', 'pycodestyle==2.8.0', 'pyflakes==2.4.0', 'setuptools==47.1.0', 'typing-extensions==4.2.0', 'wheel==0.37.1', 'zipp==3.8.0']
Traceback (most recent call last):
  File "examples/example_3.py", line 7, in <module>
    from coolpackage import AwesomeEntry, CoolAwesome
ImportError: cannot import name 'AwesomeEntry' from 'coolpackage' (unknown location)

Stuff I Tried Already

Miscellaneous stuff:

Similar questions that don't seem to help:

Related Output, i.e., hurling things at the wall and hoping something sticks

Output of #4 (python3 setup.py bdist_wheel):

running bdist_wheel
running build
running build_py
package init file 'src/__init__.py' not found (or not a regular file)
creating build
creating build/lib
creating build/lib/coolpackage
copying src/ __init__.py -> build/lib/coolpackage
copying src/coolpackage.py -> build/lib/coolpackage
running egg_info
creating coolpackage.egg-info
writing coolpackage.egg-info/PKG-INFO
writing dependency_links to coolpackage.egg-info/dependency_links.txt
writing top-level names to coolpackage.egg-info/top_level.txt
writing manifest file 'coolpackage.egg-info/SOURCES.txt'
reading manifest file 'coolpackage.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'coolpackage.egg-info/SOURCES.txt'
copying src/coolpackage.pyi -> build/lib/coolpackage
installing to build/bdist.macosx-10.9-x86_64/wheel
running install
running install_lib
creating build/bdist.macosx-10.9-x86_64
creating build/bdist.macosx-10.9-x86_64/wheel
creating build/bdist.macosx-10.9-x86_64/wheel/coolpackage
copying build/lib/coolpackage/ __init__.py -> build/bdist.macosx-10.9-x86_64/wheel/coolpackage
copying build/lib/coolpackage/coolpackage.pyi -> build/bdist.macosx-10.9-x86_64/wheel/coolpackage
copying build/lib/coolpackage/coolpackage.py -> build/bdist.macosx-10.9-x86_64/wheel/coolpackage
running install_egg_info
Copying coolpackage.egg-info to build/bdist.macosx-10.9-x86_64/wheel/coolpackage-0.0.2-py3.7.egg-info
running install_scripts
creating build/bdist.macosx-10.9-x86_64/wheel/coolpackage-0.0.2.dist-info/WHEEL
creating 'dist/coolpackage-0.0.2-py3-none-any.whl' and adding 'build/bdist.macosx-10.9-x86_64/wheel' to it
adding 'coolpackage/ __init__.py'
adding 'coolpackage/coolpackage.py'
adding 'coolpackage/coolpackage.pyi'
adding 'coolpackage-0.0.2.dist-info/METADATA'
adding 'coolpackage-0.0.2.dist-info/WHEEL'
adding 'coolpackage-0.0.2.dist-info/top_level.txt'
adding 'coolpackage-0.0.2.dist-info/RECORD'
removing build/bdist.macosx-10.9-x86_64/wheel

The package init file 'src/__init__.py' not found (or not a regular file) error is kind of sus, but I think that might just be because it's blank?

Output of pip freeze after pip installing the wheel:

coolpackage @ file:///Users/myverycoolusername/Repos/coolcoolcool/dist/coolpackage-0.0.2-py3-none-any.whl
flake8==4.0.1
importlib-metadata==4.2.0
mccabe==0.6.1
pycodestyle==2.8.0
pyflakes==2.4.0
typing-extensions==4.2.0
zipp==3.8.0

which pip, which python, etc, all within the same venv

>which python
/Users/myverycoolusername/Repos/venv/bin/python
>which python3
/Users/myverycoolusername/Repos/venv/bin/python3
>python --version
Python 3.7.9
>python3 --version
Python 3.7.9
>which pip
/Users/myverycoolusername/Repos/venv/bin/pip
>which pip3
/Users/myverycoolusername/Repos/venv/bin/pip3
>pip --version
pip 20.1.1 from /Users/myverycoolusername/Repos/venv/lib/python3.7/site-packages/pip (python 3.7)
>pip3 --version
pip 20.1.1 from /Users/myverycoolusername/Repos/venv/lib/python3.7/site-packages/pip (python 3.7)

I do not have a PYTHONPATH environmental variable and afaik I should not need one because I am in a venv.

Upvotes: 3

Views: 893

Answers (1)

Zip
Zip

Reputation: 71

Thanks to @sinoroc and a bit of staring at this answer, I figured this out. The following changes were needed:

  • removing __init__.py at the root
  • renaming src to coolpackage
  • removing package_dir from source.py
  • in example_3.py, from coolpackage import coolpackage, then access AwesomeEntry as coolpackage.AwesomeEntry

Upvotes: 3

Related Questions