Python 如何从同一文件夹中的模块导入函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39233077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to import a function from a module in the same folder?
提问by Oleksiy
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:
posts_run.py
有两个功能,get_all_posts
和retrieve_posts
,所以我尝试导入get_all_posts
:
from posts_run import get_all_posts
Python 3.5 gives the error:
Python 3.5 给出了错误:
ImportError: cannot import name 'get_all_posts'
Main.py contains following rows of code:
Main.py 包含以下几行代码:
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.
然后我需要将 api 导入到函数中,所以我有能力获得对 vk 的 API 调用。
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?
api - 是api = vk.API(session)
main.py 中的一个。absolute_url 和 fullname 也存储在 main.py 中。我在 Windows 7 上使用 PyCharm 2016.1,在 virtualenv 中使用 Python 3.5 x64。如何导入此功能?
回答by Sardorbek Imomaliev
You need to add __init__.py
in your core folder. You getting this error because python does not recognise your folder as python package
您需要添加__init__.py
到您的核心文件夹中。您收到此错误是因为 python 无法将您的文件夹识别为python 包
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
回答by Danielle M.
MyFile.py:
我的文件.py:
def myfunc():
return 12
start python interpreter:
启动python解释器:
>>> from MyFile import myFunc
>>> myFunc()
12
Alternatively:
或者:
>>> import MyFile
>>> MyFile.myFunc()
12
Does this not work on your machine?
这在您的机器上不起作用吗?
回答by A. N. Other
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
可以从这个问题中找到一个作弊解决方案(问题是为什么使用 sys.path.append(path) 而不是 sys.path.insert(1, path)?)。基本上你做以下
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...
这将解决当您在 PyCharm 2016.1 中运行它时,它可能位于与您期望的不同的当前目录中...
回答by theBugger
Python doesn't find the module to import because it is executed from another directory.
Python 找不到要导入的模块,因为它是从另一个目录执行的。
Open a terminal and cd into the script's folder, then execute python from there.
打开终端并 cd 进入脚本的文件夹,然后从那里执行 python。
Run this code in your script to print from where python is being executed from:
在脚本中运行此代码以从执行 python 的位置打印:
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
将上面的代码放在位于的test.py
文件中C:\folder\test.py
open a terminal and type
打开终端并输入
python3 C:\folder\test.py
This will output the base directory of python executable
这将输出 python 可执行文件的基目录
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
这将输出C:\folder\
. 所以如果你有其他模块在folder
导入它们应该不是问题
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
我通常会写一个 bash/batch 脚本来 cd 到目录中并启动我的程序。这允许对主机产生零影响