question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Read Version Number from the Package in setup.cfg

See original GitHub issue

Hi,

I am trying to make a project to be setup.cfg project. In the old version, we letted the setup.py read a version number from a file inside the package.

def get_version() -> dict:
    version = {}
    with open(Path(__file__).parent / 'sciunit' / 'version.py') as f:
        exec(f.read(), version)
    return version['__version__']

setup(
    ...
    version=get_version(),
    ...
    )

I am wondering how can I achieve the same thing in setup.cfg file? Is it possible?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
jimratliffcommented, May 24, 2022

I’ve answered this question at https://stackoverflow.com/questions/72357031/set-version-of-module-from-a-file-when-configuring-setuptools-using-setup

Method 1:

  • Create a __version__.py file at the same level as __init__ of your import package.
  • Add to that file: __version__ = '0.0.2'
  • In __init__.py, add from . __version__ import __version__.
  • In setup.cfg, use version = attr: my_package.__version__ Method 2:
  • Create a VERSION file at the same level as __init__ of your import package.
  • Add to that file only: 0.0.2
    • Importantly: Don’t enclose in quotation marks. NO OTHER TEXT. No comments
  • In setup.cfg use: version = file: src/my_package/VERSION
    • Note that the root for the relative path is the project directory.
3reactions
homeworkprodcommented, Feb 7, 2021

Hi, I had the same question as well.

In the docs, there is the section “Specifying Your Project’s Version”. But it didn’t led me to the only spot I could find that includes the answer, which is “Configuring setup() using setup.cfg files” (specifically, the config example’s [metadata] section). I guess it makes sense to link to there from the first section I mentioned?

As for the example itself, which is:

[metadata]
# …
version = attr: src.VERSION

Is this really a practical example? As I understand it, src here should be the name of an actual Python package. However, the whole example seems to be based on a src/ project layout. But src/ shouldn’t include a __init__.py file to make it a package, right?

So I assume a better example would be:

version = attr: yourpackage.VERSION
Read more comments on GitHub >

github_iconTop Results From Across the Web

Single-sourcing the package version
There are many techniques to maintain a single source of truth for the version number of your project: Read the file in setup.py...
Read more >
How can I get the version defined in setup.py (setuptools) in ...
I've seen a case like this where py2exe would put the wrong version number on a package because the package was getting its...
Read more >
Configuring setuptools using setup.cfg files
Setuptools allows using configuration files (usually setup.cfg ) to define a package's metadata and other options that are normally supplied to the setup() ......
Read more >
Extracting setuptools version numbers from your git repository
Create a new maintenance branch for 1.3: $ git checkout -b maint-1.3 master · Update the setup.cfg file to remove the tag_build and...
Read more >
Releasing Your Package
If you didn't you will need to update your setup.cfg file as well as using git tag . Incrementing Version Numbers¶. When you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found