How to import a function from a module in the same folder?
<p>I am trying to separate my script into several files with functions, so I moved some functions into separate files and want to import them into one main file. The structure is:</p>

<pre><code>core/
  main.py
  posts_run.py
</code></pre>

<p><code>posts_run.py</code> has two functions, <code>get_all_posts</code> and <code>retrieve_posts</code>, so I try import <code>get_all_posts</code> with:</p>

<pre><code>from posts_run import get_all_posts
</code></pre>

<p>Python 3.5 gives the error:</p>

<pre><code>ImportError: cannot import name 'get_all_posts'
</code></pre>

<p>Main.py contains following rows of code:</p>

<pre><code>import vk
from configs import client_id, login, password
session = vk.AuthSession(scope='wall,friends,photos,status,groups,offline,messages',   app_id=client_id, user_login=login,
                     user_password=password)
api = vk.API(session)
</code></pre>

<p>Then i need to import api to functions, so I have ability to get API calls to vk.</p>

<p>Full stack trace</p>

<p><code>Traceback (most recent call last):
  File "E:/gited/vkscrap/core/main.py", line 26, in &lt;module&gt;
    from posts_run import get_all_posts
  File "E:\gited\vkscrap\core\posts_run.py", line 7, in &lt;module&gt;
    from main import api, absolute_url, fullname
  File "E:\gited\vkscrap\core\main.py", line 26, in &lt;module&gt;
    from posts_run import get_all_posts
ImportError: cannot import name 'get_all_posts'</code></p>

<p>api - is a <code>api = vk.API(session)</code> in main.py.
absolute_url and fullname are also stored in main.py.
I am using PyCharm 2016.1 on Windows 7, Python 3.5 x64 in virtualenv.
How can I import this function?</p>

<p>You need to add <code>__init__.py</code> in your core folder. You getting this error because python does not recognise your folder as <a href="https://docs.python.org/3/tutorial/modules.html#packages" rel="nofollow">python package</a></p>

<p>After that do</p>

<pre><code>from .posts_run import get_all_posts
#    ^ here do relative import
# or
from core.posts_run import get_all_posts
# because your package named 'core' and importing looks in root folder
</code></pre>

python python-3.x