简体   繁体   中英

How to import a function from a module in the same folder?

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:

core/
  main.py
  posts_run.py

posts_run.py has two functions, get_all_posts and retrieve_posts , so I try import get_all_posts with:

from posts_run import get_all_posts

Python 3.5 gives the error:

ImportError: cannot import name 'get_all_posts'

Main.py contains following rows of 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)

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

Full stack trace

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

api - is a api = vk.API(session) 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?

You need to add __init__.py in your core folder. You getting this error because python does not recognise your folder as python package

After that do

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

MyFile.py:

def myfunc():
    return 12

start python interpreter:

>>> from MyFile import myFunc
>>> myFunc()
12

Alternatively:

>>> import MyFile
>>> MyFile.myFunc()
12

Does this not work on your machine?

A cheat solution can be found from this question (question is Why use sys.path.append(path) instead of sys.path.insert(1, path)? ). Essentially you do the following

    import sys
    sys.path.insert(1, directory_path_your_code_is_in)
    import file_name_without_dot_py_at_end

This will get round that as you are running it in PyCharm 2016.1, it might be in a different current directory to what you are expecting...

Python doesn't find the module to import because it is executed from another directory.

Open a terminal and cd into the script's folder, then execute python from there.

Run this code in your script to print from where python is being executed from:

import os
print(os.getcwd())

EDIT: This is a demonstration of what I mean

Put the code above in a test.py file located at C:\\folder\\test.py

open a terminal and type

python3 C:\folder\test.py

This will output the base directory of python executable

now type

cd C:\folder
python3 test.py

This will output C:\\folder\\ . So if you have other modules in folder importing them should not be a problem

I usually write a bash/batch script to cd into the directory and start my programs. This allows to have zero-impact on host machines

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM