How to have python functions and classes available system-wide with an easy `import` statement
<h1>Introduction</h1>

<p>I thought I'd try my hand at making a python module to make some of my libraries and functions available system-wide.
Google and StackExchange provide ample "solutions" and how-toos. But for some reason none of them seem to work for me. Obviously, I'm doing something wrong. But after days of trial &amp; error I've decided to show you guys what I have and let you point out the obvious error to me ;-) Your help is much appreciated.</p>

<h1>What I have</h1>

<p>My GitHub project directory contains this tree:</p>

<pre><code>$ tree /tmp/mausy5043-common-python
/tmp/mausy5043-common-python
âââ docs
âââ gitforcepull
âââ LICENSE
âââ mausy5043funcs
âÂ Â  âââ __init__.py
âââ mausy5043libs
âÂ Â  âââ __init__.py
âÂ Â  âââ libdaemon3.py
âÂ Â  âââ libsmart3.py
âââ README.md
âââ setup.py
âââ tests
</code></pre>

<p><code>libsmart3.py</code> is a python script that offers a <code>class SmartDisk():</code>. Both <code>__init__.py</code> scripts are empty. <code>libdaemon3.py</code> is a script that contains another class. It has the same problem but I'll use the <code>libsmart3</code> module as an example here.</p>

<p>I install this project as follows:</p>

<pre><code>$ sudo python3 setup.py install
running install
running build
running build_py
creating build
creating build/lib
creating build/lib/mausy5043libs
copying mausy5043libs/libdaemon3.py -&gt; build/lib/mausy5043libs
copying mausy5043libs/libsmart3.py -&gt; build/lib/mausy5043libs
running install_lib
copying build/lib/mausy5043libs/libsmart3.py -&gt; /usr/local/lib/python3.4/dist-packages/mausy5043libs
copying build/lib/mausy5043libs/libdaemon3.py -&gt; /usr/local/lib/python3.4/dist-packages/mausy5043libs
byte-compiling /usr/local/lib/python3.4/dist-packages/mausy5043libs/libsmart3.py to libsmart3.cpython-34.pyc
byte-compiling /usr/local/lib/python3.4/dist-packages/mausy5043libs/libdaemon3.py to libdaemon3.cpython-34.pyc
running install_egg_info
Removing /usr/local/lib/python3.4/dist-packages/mausy5043_common_python-0.1dev.egg-info
Writing /usr/local/lib/python3.4/dist-packages/mausy5043_common_python-0.1dev.egg-info
</code></pre>

<h1>What doesn't work</h1>

<p>Now I want to use this class <code>SmartDisk()</code> in another python script. To do this I expected (was hoping) that I would be able to import the class directly -- thanks to the presence of the <code>__init__py</code> -- thus:</p>

<pre><code>$ python3
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; from mausy5043libs import SmartDisk
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
ImportError: cannot import name 'SmartDisk'
&gt;&gt;&gt;
</code></pre>

<p><strong>OR</strong></p>

<pre><code>&gt;&gt;&gt; from libsmart3 import SmartDisk
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
ImportError: No module named 'libsmart3'
&gt;&gt;&gt;
</code></pre>

<h1>How I don't want it to work</h1>

<p>What <strong>does</strong> work is this:</p>

<p><code>&gt;&gt;&gt; from mausy5043libs.libsmart3 import SmartDisk</code></p>

<p>but this is not how it was advertised (ref: <a href="http://mikegrouchy.com/blog/2012/05/be-pythonic-__init__py.html" rel="nofollow">http://mikegrouchy.com/blog/2012/05/be-pythonic-__init__py.html</a> )</p>

<h1>What else did I try</h1>

<p>I've tried to add <code>import libsmart3</code> in <code>mausy5043libs/__init__.py</code>. Doesn't work.</p>

<p>I've added <code>from libsmart3 import SmartDisk</code> in <code>mausy5043libs/__init__py</code>. No joy.</p>

<p>I've added an empty <code>__init__.py</code> in the project root under the assumption that python needs this. Didn't help.</p>

<p><strong>EDIT</strong>
changes made: to <code>mausy5043libs/__init__py</code> as suggested by @user2357112</p>

<pre><code>$ cat mausy5043libs/__init__.py
from .libsmart3 import SmartDisk 
$ sudo python3 setup.py install 
[... output removed ...] 
$ python3
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
&gt;&gt;&gt; from mausy5043libs import SmartDisk
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
ImportError: cannot import name 'SmartDisk'
</code></pre>

<p><strong>EDIT 2</strong> : It turns out the solution provided by @user2357112 <em>does</em> indeed work. There was, obviously, a small oversight on my part. In <code>setup.py</code> the entry <code>mausy5043libs/__init__</code> was missing in the <code>py_modules</code> list.</p>

<p>You completely misread the sources you were working off of. If you want to be able to import <code>SmartDisk</code> directly from the <code>mausy5043libs</code> package, then <code>mausy5043libs/__init__.py</code> needs to import <code>SmartDisk</code>:</p>

<pre><code># in mausy5043libs/__init__.py
from .libsmart3 import SmartDisk
</code></pre>

<p>Then users of the package can import it the way you want:</p>

<pre><code># in code that wants to use SmartDisk
from mausy5043libs import SmartDisk
</code></pre>

<p>The article you linked includes this information under "What goes in <code>__init__.py</code>?", although they use pre-Python 3 implicit relative import syntax.</p>

python python-3.x python-import