简体   繁体   English

如何从Idle的包中运行python文件?

[英]how to run a python file in a package from Idle?

I have a folder (python package): 我有一个文件夹(python软件包):

app/
   __init__.py
   bases.py
   store.py

in store.py I have a the the code store.py我有一个代码

from .bases import BaseStore

class Store(BaseStore):
    # .. class 

Everything works when I run the full project. 当我运行整个项目时,一切正常。
How can I run the store.py file itself? 我如何运行store.py文件本身? Running the file with Idle gives an import error. 使用空闲运行文件会导致导入错误。
How can I run a script in idle which will be aware to its package? 我如何在空闲状态下运行一个脚本,该脚本会对其软件包有所了解?

I am pretty sure that this is not an IDLE issue but one of the interaction between relative imports, sys.path, the current working directory, and how you start python. 我很确定这不是一个IDLE问题,而是相对导入,sys.path,当前工作目录以及如何启动python之间的交互之一。 To find out for sure, add the following at the top of store.py . 为了确定这一点,请在store.py顶部添加以下store.py

import os, sys
print(os.getcwd())
print(sys.path)

Then run your code the two different ways. 然后以两种不同的方式运行您的代码。 Also add, at a command line 同时在命令行中添加

python /path/to/app/store.py

This list is the equivalent of how IDLE runs your code. 此列表等效于IDLE如何运行您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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