[TriPython] Problems with import after attempting to create pip installable package

David Handy david at handysoftware.com
Wed Nov 1 22:07:39 EDT 2017


Hi Steve -
 
I was a little confused reading your description, mainly because I wasn't sure when you said "package" whether you meant a distributable unit of Python code with a setup.py file, or whether you meant "an importable directory on sys.path containing an __init__.py file".
 
Is there an importable package directory named "get_random" containing an __init__.py file? Or is there instead a get_random.py module?
 
If you have an importable package directory named get_random, then the behavior you saw is expected. When you import a package, the only names you see in that package are the ones defined (or imported into) the __init__.py file itself. To see anything in any other module inside that package you have to import that explicity, in your main program file or in the __init__.py file.
 
Which form does your get_random program take? There are at least two ways to distribute Python scripts, one that uses setuptools and console entry points, and one doesn't.
 
Yes, if you shared your full setup.py file with us, that would probably answer those questions and more, and would be helpful in diagnosing your problem. If you would share the full layout of your distributable package files, that would help too.
 
Thanks,
David H
 
 
On Wednesday, November 1, 2017 3:29pm, "Steve Gambino" <stevegambino at gmail.com> said:



> _______________________________________________
> TriZPUG mailing list
> TriZPUG at python.org
> https://mail.python.org/mailman/listinfo/trizpug
> http://tripython.org is the Triangle Python Users Group
> Hello friends,
> 
> I'm attempting to learn how to create a pip installable package. So far the
> attempt has been relatively successful but for one nagging issue. I'm
> hoping if I describe the issue here clearly enough that someone can offer
> pointers to help me get to a resolution.
> 
> I've created the package (named get_random) successfully with setuptools
> and have been able to do a pip install of it in a virtualenv.
> 
> The package "get_random" contains a trivial program also named
> "get_random". The program, "get_random", imports one module, 'random', and
> it has the functions, main(), get_random() and print_random(). After the
> pip install of the get_random package, I can call the get_random program
> from the command line in the virtualenv and get the expected results. In
> otherwords, if I run the following in a terminal I get a resulting random
> number, like so:
> 
> sgambino-m1:~/vnv_for_pip > get_random
> 0.0259274305036
> 
> However, if I try to import the get_random package into a python REPL I do
> not see
> a complete list of what is in the package. I only see the following:
> 
> >>> dir(get_random)
> >>> ['__builtins__', '__doc__', '__file__', '__name__', '__package__',
> '__path__']
> 
> but I was expecting to see:
> 
> >>> dir(get_random)
> ['__builtins__', '__doc__', '__file__', '__name__', '__package__',
> 'get_random', 'main', 'print_random', 'random']
> 
> If I try to import the get_random package into a script and run the script,
> I get the following error:
> 
> ncralsgambino-m1:~/vnv_for_pip > ./try_get_random.py
> Traceback (most recent call last):
> File "./try_get_random.py", line 5, in <module>
> get_random.print_random()
> AttributeError: 'module' object has no attribute 'print_random'
> 
> Here is some more detail from the REPL that may give a clue. I'm wondering
> if there is something in the setup.py that I haven't
> defined correctly or not defined at all. I could share that if someone
> thinks it is worth a look. The setup.py that I started with
> was the one that is provided from https://github.com/pypa/sampleproject
> 
> Python 2.7.10 (default, Oct 23 2015, 19:19:21)
> [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import get_random
> >>> dir(get_random)
> ['__builtins__', '__doc__', '__file__', '__name__', '__package__',
> '__path__']
> >>> print(get_random.__file__)
> /Users/sgambino/vnv_for_pip/lib/python2.7/site-packages/get_random/__init__.pyc
> >>> print(get_random.__package__)
> None
> >>> print(get_random.__path__)
> ['/Users/sgambino/vnv_for_pip/lib/python2.7/site-packages/get_random']
> 
> If you have bothered to read this far, I hope this has made some sense and
> I thank you for any ideas you may be willing to share!
> Steve Gambino
> --
> Best,
> Steve
> Hello friends,
> I'm attempting to learn how to create a pip installable package. So far
> the attempt has been relatively successful but for one nagging issue. I'm
> hoping if I describe the issue here clearly enough that someone can offer
> pointers to help me get to a resolution.
> I've created the package (named get_random) successfully with setuptools
> and have been able to do a pip install of it in a virtualenv.
> The package "get_random" contains a trivial program also named
> "get_random". The program, "get_random", imports one module, 'random', and
> it has the functions, main(), get_random() and print_random(). After the
> pip install of the get_random package, I can call the get_random program
> from the command line in the virtualenv and get the expected results. In
> otherwords, if I run the following in a terminal I get a resulting random
> number, like so:
> sgambino-m1:~/vnv_for_pip > get_random
> 0.0259274305036
> However, if I try to import the get_random package into a python REPL I do
> not see
> a complete list of what is in the package. I only see the following:
> >>> dir(get_random)
> >>> ['__builtins__', '__doc__', '__file__', '__name__',
> '__package__',
> '__path__']
> 
> but I was expecting to see:
> 
> >>> dir(get_random)
> ['__builtins__', '__doc__', '__file__', '__name__', '__package__',
> 'get_random', 'main', 'print_random', 'random']
> 
> If I try to import the get_random package into a script and run the
> script, I get the following error:
> 
> ncralsgambino-m1:~/vnv_for_pip > ./try_get_random.py
> Traceback (most recent call last):
> ** File "./try_get_random.py", line 5, in <module>
> ****** get_random.print_random()
> AttributeError: 'module' object has no attribute 'print_random'
> 
> Here is some more detail from the REPL that may give a clue.** I'm
> wondering if there is something in the setup.py that I haven't
> defined correctly or not defined at all. I could share that if someone
> thinks it is worth a look. The setup.py that I started with
> was the one that is provided from [1]https://github.com/pypa/sampleproject
> Python 2.7.10 (default, Oct 23 2015, 19:19:21)
> [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import get_random
> >>> dir(get_random)
> ['__builtins__', '__doc__', '__file__', '__name__', '__package__',
> '__path__']
> >>> print(get_random.__file__)
> /Users/sgambino/vnv_for_pip/lib/python2.7/site-packages/get_random/__init__.pyc
> >>> print(get_random.__package__)
> None
> >>> print(get_random.__path__)
> ['/Users/sgambino/vnv_for_pip/lib/python2.7/site-packages/get_random']
> If you have bothered to read this far, I hope this has made some sense and
> I thank you for any ideas you may be willing to share!
> Steve Gambino
> --
> Best,**
> Steve
> 
> References
> 
> Visible links
> 1. https://github.com/pypa/sampleproject
> 
-------------- next part --------------
   Hi Steve -



   I was a little confused reading your description, mainly because I wasn't
   sure when you said "package" whether you meant a distributable unit of
   Python code with a setup.py file, or whether you meant "an importable
   directory on sys.path containing an __init__.py file".



   Is there an importable package directory named "get_random" containing an
   __init__.py file? Or is there instead a get_random.py module?



   If you have an importable package directory named get_random, then the
   behavior you saw is expected. When you import a package, the only names
   you see in that package are the ones defined (or imported into) the
   __init__.py file itself. To see anything in any other module inside that
   package you have to import that explicity, in your main program file or in
   the __init__.py file.



   Which form does your get_random program take? There are at least two ways
   to distribute Python scripts, one that uses setuptools and console entry
   points, and one doesn't.



   Yes, if you shared your full setup.py file with us, that would probably
   answer those questions and more, and would be helpful in diagnosing your
   problem. If you would share the full layout of your distributable package
   files, that would help too.



   Thanks,

   David H





   On Wednesday, November 1, 2017 3:29pm, "Steve Gambino"
   <stevegambino at gmail.com> said:

   > _______________________________________________
   > TriZPUG mailing list
   > TriZPUG at python.org
   > https://mail.python.org/mailman/listinfo/trizpug
   > http://tripython.org is the Triangle Python Users Group
   > Hello friends,
   >
   > I'm attempting to learn how to create a pip installable package. So far
   the
   > attempt has been relatively successful but for one nagging issue. I'm
   > hoping if I describe the issue here clearly enough that someone can
   offer
   > pointers to help me get to a resolution.
   >
   > I've created the package (named get_random) successfully with setuptools
   > and have been able to do a pip install of it in a virtualenv.
   >
   > The package "get_random" contains a trivial program also named
   > "get_random". The program, "get_random", imports one module, 'random',
   and
   > it has the functions, main(), get_random() and print_random(). After the
   > pip install of the get_random package, I can call the get_random program
   > from the command line in the virtualenv and get the expected results. In
   > otherwords, if I run the following in a terminal I get a resulting
   random
   > number, like so:
   >
   > sgambino-m1:~/vnv_for_pip > get_random
   > 0.0259274305036
   >
   > However, if I try to import the get_random package into a python REPL I
   do
   > not see
   > a complete list of what is in the package. I only see the following:
   >
   > >>> dir(get_random)
   > >>> ['__builtins__', '__doc__', '__file__', '__name__', '__package__',
   > '__path__']
   >
   > but I was expecting to see:
   >
   > >>> dir(get_random)
   > ['__builtins__', '__doc__', '__file__', '__name__', '__package__',
   > 'get_random', 'main', 'print_random', 'random']
   >
   > If I try to import the get_random package into a script and run the
   script,
   > I get the following error:
   >
   > ncralsgambino-m1:~/vnv_for_pip > ./try_get_random.py
   > Traceback (most recent call last):
   > File "./try_get_random.py", line 5, in <module>
   > get_random.print_random()
   > AttributeError: 'module' object has no attribute 'print_random'
   >
   > Here is some more detail from the REPL that may give a clue. I'm
   wondering
   > if there is something in the setup.py that I haven't
   > defined correctly or not defined at all. I could share that if someone
   > thinks it is worth a look. The setup.py that I started with
   > was the one that is provided from https://github.com/pypa/sampleproject
   >
   > Python 2.7.10 (default, Oct 23 2015, 19:19:21)
   > [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
   > Type "help", "copyright", "credits" or "license" for more information.
   > >>> import get_random
   > >>> dir(get_random)
   > ['__builtins__', '__doc__', '__file__', '__name__', '__package__',
   > '__path__']
   > >>> print(get_random.__file__)
   >
   /Users/sgambino/vnv_for_pip/lib/python2.7/site-packages/get_random/__init__.pyc
   > >>> print(get_random.__package__)
   > None
   > >>> print(get_random.__path__)
   > ['/Users/sgambino/vnv_for_pip/lib/python2.7/site-packages/get_random']
   >
   > If you have bothered to read this far, I hope this has made some sense
   and
   > I thank you for any ideas you may be willing to share!
   > Steve Gambino
   > --
   > Best,
   > Steve
   > Hello friends,
   > I'm attempting to learn how to create a pip installable package. So far
   > the attempt has been relatively successful but for one nagging issue.
   I'm
   > hoping if I describe the issue here clearly enough that someone can
   offer
   > pointers to help me get to a resolution.
   > I've created the package (named get_random) successfully with setuptools
   > and have been able to do a pip install of it in a virtualenv.
   > The package "get_random" contains a trivial program also named
   > "get_random". The program, "get_random", imports one module, 'random',
   and
   > it has the functions, main(), get_random() and print_random(). After the
   > pip install of the get_random package, I can call the get_random program
   > from the command line in the virtualenv and get the expected results. In
   > otherwords, if I run the following in a terminal I get a resulting
   random
   > number, like so:
   > sgambino-m1:~/vnv_for_pip > get_random
   > 0.0259274305036
   > However, if I try to import the get_random package into a python REPL I
   do
   > not see
   > a complete list of what is in the package. I only see the following:
   > >>> dir(get_random)
   > >>> ['__builtins__', '__doc__', '__file__', '__name__',
   > '__package__',
   > '__path__']
   >
   > but I was expecting to see:
   >
   > >>> dir(get_random)
   > ['__builtins__', '__doc__', '__file__', '__name__', '__package__',
   > 'get_random', 'main', 'print_random', 'random']
   >
   > If I try to import the get_random package into a script and run the
   > script, I get the following error:
   >
   > ncralsgambino-m1:~/vnv_for_pip > ./try_get_random.py
   > Traceback (most recent call last):
   > ** File "./try_get_random.py", line 5, in <module>
   > ****** get_random.print_random()
   > AttributeError: 'module' object has no attribute 'print_random'
   >
   > Here is some more detail from the REPL that may give a clue.** I'm
   > wondering if there is something in the setup.py that I haven't
   > defined correctly or not defined at all. I could share that if someone
   > thinks it is worth a look. The setup.py that I started with
   > was the one that is provided from
   [1]https://github.com/pypa/sampleproject
   > Python 2.7.10 (default, Oct 23 2015, 19:19:21)
   > [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
   > Type "help", "copyright", "credits" or "license" for more information.
   > >>> import get_random
   > >>> dir(get_random)
   > ['__builtins__', '__doc__', '__file__', '__name__', '__package__',
   > '__path__']
   > >>> print(get_random.__file__)
   >
   /Users/sgambino/vnv_for_pip/lib/python2.7/site-packages/get_random/__init__.pyc
   > >>> print(get_random.__package__)
   > None
   > >>> print(get_random.__path__)
   > ['/Users/sgambino/vnv_for_pip/lib/python2.7/site-packages/get_random']
   > If you have bothered to read this far, I hope this has made some sense
   and
   > I thank you for any ideas you may be willing to share!
   > Steve Gambino
   > --
   > Best,**
   > Steve
   >
   > References
   >
   > Visible links
   > 1. https://github.com/pypa/sampleproject
   >


More information about the TriZPUG mailing list