我有这个目录结构
parent/
child/
__init__.py
sibling1.py
sibling2.py
setup.py
这在我的setup.py
from setuptools import setup
setup(name='parent',
version='0.1',
packages=['child'],
scripts = ['child/sibling1.py'],
)
并且sibling1.py
我有from . import sibling2
。
但是当我在控制台中运行sibling1.py
时,我得到了
Attempted relative import in non-package
我也尝试使用from child import sibling2
,但它不起作用。
我做错了什么?
修改
我修复了将entry_points = {'console_script' : ['sibling1 = child:main']}
添加到setup()
setup.py
的问题
所以我必须在我的main
中编写一个名为child/__init__.py
的方法,然后我在from . import sibling2
执行sibling1
我在控制台中运行import time
from datetime import datetime
class StatsMiddleware(object):
duration = 0
def process_request(self, request):
# Store the start time when the request comes in.
request.start_time = time.time()
def process_response(self, request, response):
# Calculate and output the page generation duration
# Get the start time from the request and calculate how long
# the response took.
self.duration = time.time() - request.start_time
response["x-server-time"] = datetime.now().strftime("%d/%m/%Y %H:%M")
response.content = response.content.replace(b"server_time_777", str.encode(response["x-server-time"]))
response["x-page-generation-duration-ms"] = '{:.3f}'.format(self.duration)
response.content = response.content.replace(b"gen_duration_time_777", str.encode(response["x-page-generation-duration-ms"]))
return response
,一切正常!
答案 0 :(得分:0)
你不必说from <dir> import <file>
。只要所有文件夹都在你的python路径中,你就可以说import sibling2
。 from / import语法用于将特定元素从文件/类导入当前命名空间......不需要引用父目录。