推荐学习书目
› 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 · Dec 21, 2023 · 3103 views
    This topic created in 1009 days ago, the information mentioned may be changed or developed.

    我尝试建立一个 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
       Dec 21, 2023
    呃,搞定了。
    要先 `poetry install`,这样正在编写的项目就能被访问了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   849 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:45 · PVG 04:45 · LAX 13:45 · JFK 16:45
    ♥ Do have faith in what you're doing.