V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
hiwenvv
V2EX  ›  Python

如何访问正在编写的包的版本号?

  •  
  •   hiwenvv · 134 天前 · 1091 次点击
    这是一个创建于 134 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我尝试建立一个 Python 项目。我使用 Poetry 来管理依赖。在我的 pyproject.toml 里,有下列内容:

    [tool.poetry]
    name = "gitdida"
    version = "0.2.0"
    

    我的目录结构为:

    project-root/
    |-- pyproject.toml
    |-- src/
    |   |-- gitdida/
    |       |-- __init__.py
    |-- tests/
    |   |-- test_script.py
    

    我希望能在项目的代码文件里访问到 version。但是,Poetry 自身没有提供代码访问途径。我查找网络上的信息,chatGPT 说可以通过importlib.metadata取得版本号:

    from importlib.metadata import version
    
    def test_version():
        expected = "0.2.0"
        assert version("gitdida") == expected
    
    

    但是,我执行测试后得到错误提示:importlib.metadata.PackageNotFoundError: No package metadata was found for gitdida

    为什么会找不到 gitdida 包呢?我卡这里很久了

    hiwenvv
        1
    hiwenvv  
    OP
       134 天前
    呃,搞定了。
    要先 `poetry install`,这样正在编写的项目就能被访问了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1004 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 23:52 · PVG 07:52 · LAX 16:52 · JFK 19:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.