我有一个constants.py位于项目根目录,然后我在config subdir中有一个sub lib config,在subdir下的文件将导入constants.py。 它已经可以完美运行,只是无法生成可以生成pex文件的BUILD文件。
方法1:我将constants.py添加为一个依赖项
python_library(
sources = rglobs(‘config/*.py’),
name = ‘config’,
dependencies=[
‘3rdparty/python:requests’,
‘3rdparty/python:sh’,
‘constants.py’,
],
compatibility = ‘CPython>=2.7,<3’,
)
python_binary(
name = 'test',
source='main.py',
dependencies = [
'3rdparty/python:requests',
'3rdparty/python:Flask',
'3rdparty/python:CherryPy',
':config',
],
zip_safe=False,
compatibility = 'CPython>=2.7,<3',
)
它给出错误:异常消息:目录“ Dir(path ='constants.py')”不包含构建文件。
方法2:我也尝试像这样将常量添加为一个源文件:
python_library(
sources = rglobs(‘config/*.py’, 'constants.py'),
name = ‘config’,
dependencies=[
‘3rdparty/python:requests’,
‘3rdparty/python:sh’,
],
compatibility = ‘CPython>=2.7,<3’,
)
python_binary(
name = 'test',
source='main.py',
dependencies = [
'3rdparty/python:requests',
'3rdparty/python:Flask',
'3rdparty/python:CherryPy',
':config',
],
zip_safe=False,
compatibility = 'CPython>=2.7,<3',
)
然后它给了我这个错误:ImportError:没有名为常量的模块
方法3:我还尝试将除main.py之外的所有py文件定义为其他资源,然后在我的主库中将allother定义为一个依赖项
resources(
name='allother',
sources=rglobs('config/*', 'constants.py'),
)
python_binary(
name = 'test',
source='main.py',
dependencies = [
'3rdparty/python:requests',
'3rdparty/python:Flask',
'3rdparty/python:CherryPy',
':allother',
],
zip_safe=False,
compatibility = 'CPython>=2.7,<3',
)
./ main.py:
from config.config import Config
def main():
vs_manager = Config()
result = vs_manager.run()
return 0
if __name__ == "__main__":
main()
./ constants.py:
pi = 3.14
./ config / config.py:
import constants
class Config:
def __init__(self):
self.value = constants.pi
def run(self):
print(self.value)
我希望上述方法之一应该为我的项目生成一个pex文件。