Using a folder for personal modules in Python
<p>I have created a folder named <code>C:\Python27\Lib\site-packages\perso</code> and inside I have put a file <code>mymodule.py</code>. The goal is to have this module accessible from any future Python script.</p>

<p>Let's do a <code>D:\My Documents\test.py</code> file:</p>

<pre><code>import mymodule   #fails
import perso.mymodule   #fails
</code></pre>

<p><strong>Why does it fail? How to import a module from <code>C:\Python27\Lib\site-packages\perso</code>? What are the best practice for using user-modules in all Python scripts of the same computer?</strong></p>

<p><strong>Python Modules:</strong></p>

<p>In order to create a module you can do the following:</p>

<ol>
<li>under <code>&lt;Python_DIR&gt;\Lib\site-packages</code>:<br>
put you directory <code>&lt;module&gt;</code>. This directory contains your classes.</li>
<li>In <code>&lt;Python_DIR&gt;\Lib\site-packages\&lt;module&gt;</code> put an init file <code>__init__.py</code>,<br>
This file define what's in the directory, and may apply some logic if needed.<br>
for example: 
   <code>__all__ = ["class_1", "class_2",]</code>.</li>
</ol>

<p>Than, to import:<br></p>

<pre><code>from &lt;your_module&gt; import &lt;your_class&gt;
</code></pre>

<p>For more information, read <a href="https://docs.python.org/2/tutorial/modules.html" rel="nofollow">this</a>.</p>

python module python-module