PyPI [resolved]

"Please leave a message at the beep, we will get back to you when your support contract expires."

Moderators: phlip, Moderators General, Prelates

User avatar
LucasBrown
Posts: 299
Joined: Thu Apr 15, 2010 2:57 am UTC
Location: Poway, CA

PyPI [resolved]

Postby LucasBrown » Thu Mar 01, 2018 6:22 pm UTC

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?
Last edited by LucasBrown on Thu Mar 01, 2018 10:58 pm UTC, edited 1 time in total.

DavidSh
Posts: 143
Joined: Thu Feb 25, 2016 6:09 pm UTC

Re: PyPI

Postby DavidSh » Thu Mar 01, 2018 7:57 pm UTC

What do you get from dir(labmath) after you have imported it?

User avatar
LucasBrown
Posts: 299
Joined: Thu Apr 15, 2010 2:57 am UTC
Location: Poway, CA

Re: PyPI

Postby LucasBrown » Thu Mar 01, 2018 8:33 pm UTC

Code: Select all

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

User avatar
Xenomortis
Not actually a special flower.
Posts: 1422
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: PyPI

Postby Xenomortis » Thu Mar 01, 2018 10:01 pm UTC

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>
Image

User avatar
LucasBrown
Posts: 299
Joined: Thu Apr 15, 2010 2:57 am UTC
Location: Poway, CA

Re: PyPI

Postby LucasBrown » Thu Mar 01, 2018 10:31 pm UTC

... 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?

User avatar
pogrmman
Posts: 540
Joined: Wed Jun 29, 2016 10:53 pm UTC
Location: Probably outside

Re: PyPI

Postby pogrmman » Thu Mar 01, 2018 10:36 pm UTC

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).
Last edited by pogrmman on Thu Mar 01, 2018 10:41 pm UTC, edited 1 time in total.

User avatar
LucasBrown
Posts: 299
Joined: Thu Apr 15, 2010 2:57 am UTC
Location: Poway, CA

Re: PyPI

Postby LucasBrown » Thu Mar 01, 2018 10:41 pm UTC

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.

User avatar
Xenomortis
Not actually a special flower.
Posts: 1422
Joined: Thu Oct 11, 2012 8:47 am UTC

Re: PyPI

Postby Xenomortis » Thu Mar 01, 2018 11:00 pm UTC

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
Image

User avatar
Xanthir
My HERO!!!
Posts: 5327
Joined: Tue Feb 20, 2007 12:49 am UTC
Location: The Googleplex
Contact:

Re: PyPI [resolved]

Postby Xanthir » Sun Mar 11, 2018 4:52 am UTC

I wrote myself a guide to how packages/modules work, and you might find it helpful too https://www.xanthir.com/b4pt0.
(defun fibs (n &optional (a 1) (b 1)) (take n (unfold '+ a b)))


Return to “The Help Desk”

Who is online

Users browsing this forum: No registered users and 7 guests