Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot import name '__version__' from 'pylode' #119

Closed
VladimirAlexiev opened this issue Apr 13, 2021 · 10 comments
Closed

cannot import name '__version__' from 'pylode' #119

VladimirAlexiev opened this issue Apr 13, 2021 · 10 comments

Comments

@VladimirAlexiev
Copy link

VladimirAlexiev commented Apr 13, 2021

I use windows 10, cygwin, and python 3.7.

It seems I have pylode 2.8.5:

$ pip install pylode
Requirement already satisfied: pylode in c:\users\vladimir.alexiev.000\appdata\local\programs\python\python37\lib\site-packages (2.8.5)
Requirement already satisfied: rdflib-jsonld>=0.5.0 in c:\users\vladimir.alexiev.000\appdata\local\programs\python\python37\lib\site-packages (from pylode) (0.5.0)
Requirement already satisfied: markdown in c:\users\vladimir.alexiev.000\appdata\local\programs\python\python37\lib\site-packages (from pylode) (3.3.3)
Requirement already satisfied: rdflib>=5.0.0 in c:\users\vladimir.alexiev.000\appdata\local\programs\python\python37\lib\site-packages (from pylode) (5.0.0)
Requirement already satisfied: jinja2 in c:\users\vladimir.alexiev.000\appdata\local\programs\python\python37\lib\site-packages (from pylode) (2.11.2)
Requirement already satisfied: requests in c:\users\vladimir.alexiev.000\appdata\local\programs\python\python37\lib\site-packages (from pylode) (2.21.0)
Requirement already satisfied: pyparsing in c:\users\vladimir.alexiev.000\appdata\local\programs\python\python37\lib\site-packages (from rdflib>=5.0.0->pylode) (2.3.1)
Requirement already satisfied: six in c:\users\vladimir.alexiev.000\appdata\local\programs\python\python37\lib\site-packages (from rdflib>=5.0.0->pylode) (1.12.0)
Requirement already satisfied: isodate in c:\users\vladimir.alexiev.000\appdata\local\programs\python\python37\lib\site-packages (from rdflib>=5.0.0->pylode) (0.6.0)
Requirement already satisfied: MarkupSafe>=0.23 in c:\users\vladimir.alexiev.000\appdata\local\programs\python\python37\lib\site-packages (from jinja2->pylode) (1.1.1)
Requirement already satisfied: importlib-metadata in c:\users\vladimir.alexiev.000\appdata\local\programs\python\python37\lib\site-packages (from markdown->pylode) (3.4.0)
Requirement already satisfied: zipp>=0.5 in c:\users\vladimir.alexiev.000\appdata\local\programs\python\python37\lib\site-packages (from importlib-metadata->markdown->pylode) (3.4.0)
Requirement already satisfied: typing-extensions>=3.6.4 in c:\users\vladimir.alexiev.000\appdata\local\programs\python\python37\lib\site-packages (from importlib-metadata->markdown->pylode) (3.7.4.3)
Requirement already satisfied: idna<2.9,>=2.5 in c:\users\vladimir.alexiev.000\appdata\local\programs\python\python37\lib\site-packages (from requests->pylode) (2.8)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\users\vladimir.alexiev.000\appdata\local\programs\python\python37\lib\site-packages (from requests->pylode) (3.0.4)
Requirement already satisfied: urllib3<1.25,>=1.21.1 in c:\users\vladimir.alexiev.000\appdata\local\programs\python\python37\lib\site-packages (from requests->pylode) (1.24.1)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\vladimir.alexiev.000\appdata\local\programs\python\python37\lib\site-packages (from requests->pylode) (2019.3.9)

But when I try to print the version, I get this error:

$ pylode -v
Traceback (most recent call last):
  File "c:\users\vladimir.alexiev.000\appdata\local\programs\python\python37\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\users\vladimir.alexiev.000\appdata\local\programs\python\python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\vladimir.alexiev.000\AppData\Local\Programs\Python\Python37\Scripts\pylode.exe\__main__.py", line 7, in <module>
  File "c:\users\vladimir.alexiev.000\appdata\local\programs\python\python37\lib\site-packages\pylode\cli.py", line 108, in main
    from pylode import __version__
ImportError: cannot import name '__version__' from 'pylode' (c:\users\vladimir.alexiev.000\appdata\local\programs\python\python37\lib\site-packages\pylode\__init__.py)

Same happens from cygwin bash and windows cmd

@smarie
Copy link

smarie commented Apr 26, 2021

@nicholascar may I suggest to use a well-proven pattern to handle the __version__ attribute here ?

I have it documented here: https://smarie.github.io/python-getversion/#package-versioning-best-practices

I used this pattern in 20+ python libs, it works perfectly fine. For example in makefun, pyfields or pytest-cases

Note: this patterns adds a single dependency to your project build (setup dependency, not install dependency). This dependency is setuptools_scm, it is a very stable package by the PyPa (from the author of pytest) and is used in many python packages setup today.

If you agree I can try to propose a PR

@nicholascar
Copy link
Member

Hi @smarie thanks for pointing out that versioning pattern. I've tried to implement it myself as I wanted to understand it. Also, the versioning is quite fragile in this package since the version is quoted in package code. This fragility is likely just down to my bad programming... Anyway, I think I've implemented everything so I'd be really thrilled if you could just review the code and see if it's doing what you think it should be now.

@nicholascar
Copy link
Member

@VladimirAlexiev can you please try v2.8.10 now in PyPI? I think it solves this version issue.

@smarie
Copy link

smarie commented Apr 27, 2021

@nicholascar very fine, I'll check right now !

@smarie
Copy link

smarie commented Apr 27, 2021

There is one thing that needs explanation : with this parttern the _version.py file should not be in the git files (it should be ignored). Indeed it is generated when you do python setup.py bdist_wheel or any command that trigger it, such as pip install .. This command will typically be executed just before releasing to PyPi, for example by the github action workflow.

In order for this generation process to work, your setup.py should contain the right option for setuptools_scm.

In the current version that I see on the master branch, the _version.py file seems committed, so the try except in the __init__.py will never hit the "except" part.

Let me know if this makes sense ?

@nicholascar
Copy link
Member

Yeah, I saw that pattern - to exclude _version.py - but I had some problems with import dependencies since my code uses the version number in quite a few places. Also, before tagging, I had an ugly Git version - 2.8.10-dev0wkejrewhrw or similar - which I have to avoid since the version number's printed out in HTML docs and needs to be short e.g. just 2.8.10.

I'll try and remove the _version.py file shortly and see if I can get everything to run.

@nicholascar
Copy link
Member

OK, I think it's gone now! Both GitHub and PyPI updated.

@smarie
Copy link

smarie commented Apr 27, 2021

@nicholascar great !

Note: the "ugly" version is perfectly normal. It ensures that all unique code bases have different versions. Therefore until you are "perfectly" on the tag, the version is a "working" version. If you have uncommited file changes locally, there is also an additional string added to denote this. All of this ensures that when the version number is clean, it really corresponds to a clean package built from the git tag without any mod.

@nicholascar
Copy link
Member

the "ugly" version is perfectly normal

Sure, but I just can't have it shown in the HTML documentation the program generates! I appreciate the use of the git version in dev but just have to fall back on the version_tuple for end users' display. Seems to be working fine now.

It's a nice pattern this and I think I have many other Python programs that it's actualyl mroe useful for than this one, so I'll try and roll it out in them!

Thanks!

@VladimirAlexiev can you confirm this issue is solved? If so, can you please close this Issue? Cheers, Nick

@nicholascar
Copy link
Member

This issue is long resolved so I'm closing. Please reopen if there are still problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants