Page 1 of 1

PyPI [resolved]

Posted: Thu Mar 01, 2018 6:22 pm UTC
by LucasBrown
I made myself a library of math functions in Python3. It works perfectly if I put it in the working directory; I can then import and use the functions and everything's all nice and smooth. This is a bit annoying, however, since I need to keep copying it everywhere on my machine and onto other machines, so I decided to publish it via PyPI. I packaged it up with all the fancy stuff indicated by the Python documentation and uploaded it to PyPI, resulting in this thing here, and I can even download and install it with pip3. The problems arise when I try importing stuff from it:

Attempting to access the library via import labmath produces the following:

Code: Select all

  0|lucas@blazar:~$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import labmath
>>> labmath.gcd(10, 12)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'labmath' has no attribute 'gcd'


Attempting to access the library via from labmath import * produces the following:

Code: Select all

  0|lucas@blazar:~$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from labmath import *
>>> gcd(10, 12)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'gcd' is not defined


Attempting to import specific functions produces the following:

Code: Select all

  0|lucas@blazar:~$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from labmath import gcd
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'gcd'


Can anyone help me figure out how to make this work?

Re: PyPI

Posted: Thu Mar 01, 2018 7:57 pm UTC
by DavidSh
What do you get from dir(labmath) after you have imported it?

Re: PyPI

Posted: Thu Mar 01, 2018 8:33 pm UTC
by LucasBrown

Code: Select all

>>> import labmath
>>> dir(labmath)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']

Re: PyPI

Posted: Thu Mar 01, 2018 10:01 pm UTC
by Xenomortis

Code: Select all

>>> import labmath.labmath
>>> labmath.labmath.gcd
<function gcd at 0x7f880b94f9d8>

Your package is labmath, which contains a module called labmath.
In the __init__.py of your package, put:

Code: Select all

from labmath.labmath import *

Code: Select all

>>> import labmath
>>> labmath.gcd
<function gcd at 0x7f7b70e846a8>

Re: PyPI

Posted: Thu Mar 01, 2018 10:31 pm UTC
by LucasBrown
... huh. That seems a bit weird. Is there a way to do things so that I don't have to have that line in my __init__.py, or even not have the __init__.py at all?

Re: PyPI

Posted: Thu Mar 01, 2018 10:36 pm UTC
by pogrmman
LucasBrown wrote:... huh. That seems a bit weird. Is there a way to do things so that I don't have to have that line in my __init__.py, or even not have the __init__.py at all?


I'm not entirely sure, but I was just going to come here to say that you need to import the functions you want to export in the package into __init__.py


IIRC, __init__.py just lets python know that there is a package, and the names in __init__.py go into the namespace of the package (the labmath namespace). Other files and directories in the same folder become namespaces within the main namespace. So, with a directory structure like this:

Code: Select all

foo/
    __init__.py
    foo.py
    bar/
       __init__.py
       baz.py
where both __init__.py are blank, to get to abc in foo, you'd need to do foo.foo.abc, and to get to abc in baz, you'd need to do foo.bar.baz.abc. (It's been a while since I've written any packages though, so IDK if this is right).

Re: PyPI

Posted: Thu Mar 01, 2018 10:41 pm UTC
by LucasBrown
OK. Well, labmath is now up and functioning as intended and is shamelessly self-promoted in my signature, so thank you all for your help.

Re: PyPI

Posted: Thu Mar 01, 2018 11:00 pm UTC
by Xenomortis
LucasBrown wrote:... huh. That seems a bit weird. Is there a way to do things so that I don't have to have that line in my __init__.py, or even not have the __init__.py at all?

Directories with an __init__.py file are packages - you need the __init__.py file for python to recognise your directory as a package.
Other python files within that are modules.

As the name "init" suggests, __init__.py contains the initialisation code for your package - it's entirely reasonable to expose things contained in submodules in the higher level namespace.
You can see the exact same thing in other packages - pip install mock and have a look at that's __init__.py

Re: PyPI [resolved]

Posted: Sun Mar 11, 2018 4:52 am UTC
by Xanthir
I wrote myself a guide to how packages/modules work, and you might find it helpful too https://www.xanthir.com/b4pt0.