0

I have just uploaded a Python package into pip called hqc:

https://pypi.org/project/hqc/0.0.1/

then successfully pip installed the package using:

pip install hqc

pip install hqc but I can't seem to import the package in Python Interpreter. When I do this, I get the following error message:

>>> import hqc
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import hqc
ModuleNotFoundError: No module named 'hqc'

What am I missing? Do I need to set the environment variable PATH or is it something else?

Leockl
  • 1,084
  • 6
  • 16

2 Answers2

1

In the folder where setup.py is, make a folder called hqc and put an empty file called __init__.py (thats two underscores on each side). Right now, there's no file to import so python fails when importing it.

hqc-0.0.1:

enter image description here

hqc:

enter image description here

You can test the package by going to the directory where setup.py is and running

pip install .

Before trying again, be sure to uninstall

pip uninstall hqc
donaldsa18
  • 177
  • 4
  • Should I be putting the source code for my package into the file called `__init__.py`? I currently have my source code in a file called `HQC.py` which sits in the same folder where `setup.py` is. – Leockl Jan 18 '20 at 07:02
  • 1
    You can either do that or you can move HQC.py to the hqc folder and import everything like this: from hqc.hqc import * – donaldsa18 Jan 18 '20 at 07:06
  • Ok I have put my source code into `__init__.py`, created a hqc folder where `setup.py` is and put the `__init__.py` file into this hqc folder, and it worked! Many thanks for this! The guide (https://packaging.python.org/tutorials/packaging-projects/) clearly doesn't say `__init__.py` needs to be in a folder, where this folder is in the same directory as `setup.py` – Leockl Jan 18 '20 at 08:04
0

I had a quick look at your github repository and found that the package name, i.e. your folder name is skltemplate.

So, your import will be: (verified it to be working)

import skltemplate

And found that skltemplate package exposes the following modeules: TemplateClassifier, TemplateTransformer and TemplateEstimator.

You can import the above by doing:

from skltemplate import TemplateClassifier
  • Now, if you want your package to be named as hqc, you need to rename skltemplate directory to hqc AND run the setup.py again. (You can refer this stackoverflow answer for more details).

  • Ensure that you increment the package version inside setup.py before running it.

Rahul Bharadwaj
  • 1,963
  • 1
  • 12
  • 22
  • That's strange because my github repository should be: https://github.com/leockl/helstrom-quantum-centroid-classifier. If you click on "Homepage" on the left hand side of this page, https://pypi.org/project/hqc/0.0.1/, it brings you to https://github.com/leockl/helstrom-quantum-centroid-classifier – Leockl Jan 18 '20 at 07:05
  • 1
    Yes, that's repo I ended up at. Here is an image of that: https://ibb.co/7tR9DWH – Rahul Bharadwaj Jan 18 '20 at 07:25
  • 1
    My github is a little messy at the moment, so probably that's why you got the repositories confused, apologies about this – Leockl Jan 18 '20 at 08:07