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

Unable to import package with a name different from the project name #5744

Closed
fabiobatalha opened this issue Jun 1, 2022 · 11 comments
Closed

Comments

@fabiobatalha
Copy link

I'm moving a library to use poetry. This library has a name that is different from the main package. It works well with setuptools and the function : packages=find_packages().

With poetry I have the following configuration:

Screenshot from 2022-06-01 08-55-07

But the package crossref is still not available for import.

I don't know what could be wrong.

@GenosW
Copy link

GenosW commented Jun 1, 2022

Hey, I think what you want to use is:

packages = [{ from = "src", include = "crossref" }]

@fabiobatalha
Copy link
Author

Hi @GenosW

Even with this configuration, it still not works, but now with a different error.

Screenshot from 2022-06-01 10-12-14

@GenosW
Copy link

GenosW commented Jun 1, 2022

What are you trying to do? Which command are you invoking? --> There is some context missing to effectively help you.

Did poetry install work and are you able to import your package now in that env?

Note: I'm not a poetry developer, just an avid user.

@fabiobatalha
Copy link
Author

for now I'm just trying to import the installed package.

import crossref

Yes poetry install works fine, the issue is: I can not import the package.

(.venv) fabiobatalha:~/Work/crossrefapi$ poetry install
Installing dependencies from lock file

No dependencies to install or update

Installing the current project: crossrefapi (1.6.0)

(.venv) fabiobatalha:~/Work/crossrefapi$ python
Python 3.8.10 (default, Mar 15 2022, 12:22:08) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import crossref
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/fabiobatalha/Work/crossrefapi/src/crossref/__init__.py", line 3, in <module>
    VERSION = pkg_resources.get_distribution("crossref").version
  File "/home/fabiobatalha/Work/crossrefapi/.venv/lib/python3.8/site-packages/pkg_resources/__init__.py", line 478, in get_distribution
    dist = get_provider(dist)
  File "/home/fabiobatalha/Work/crossrefapi/.venv/lib/python3.8/site-packages/pkg_resources/__init__.py", line 354, in get_provider
    return working_set.find(moduleOrReq) or require(str(moduleOrReq))[0]
  File "/home/fabiobatalha/Work/crossrefapi/.venv/lib/python3.8/site-packages/pkg_resources/__init__.py", line 909, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/home/fabiobatalha/Work/crossrefapi/.venv/lib/python3.8/site-packages/pkg_resources/__init__.py", line 795, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'crossref' distribution was not found and is required by the application
>>> 

@abn
Copy link
Member

abn commented Jun 1, 2022

@fabiobatalha recreate your virtualenv. rm -rf .venv; poetry install.

@fabiobatalha
Copy link
Author

fabiobatalha commented Jun 1, 2022

Not even with a new virtualenv.

(.venv) fabiobatalha:~/Work/crossrefapi$ poetry install
Installing dependencies from lock file

Package operations: 41 installs, 0 updates, 0 removals

  • Installing six (1.16.0)
  • Installing asttokens (2.0.5)
  • Installing executing (0.8.3)
  • Installing parso (0.8.3)
  • Installing ptyprocess (0.7.0)
  • Installing pure-eval (0.2.2)
  • Installing pyparsing (3.0.9)
  • Installing traitlets (5.2.2.post1)
  • Installing wcwidth (0.2.5)
  • Installing attrs (21.4.0)
  • Installing backcall (0.2.0)
  • Installing certifi (2022.5.18.1)
  • Installing charset-normalizer (2.0.12)
  • Installing click (8.1.3)
  • Installing decorator (5.1.1)
  • Installing idna (3.3)
  • Installing iniconfig (1.1.1)
  • Installing jedi (0.18.1)
  • Installing matplotlib-inline (0.1.3)
  • Installing mccabe (0.6.1)
  • Installing mypy-extensions (0.4.3)
  • Installing packaging (21.3)
  • Installing pathspec (0.9.0)
  • Installing pexpect (4.8.0)
  • Installing pickleshare (0.7.5)
  • Installing platformdirs (2.5.2)
  • Installing pluggy (1.0.0)
  • Installing prompt-toolkit (3.0.29)
  • Installing py (1.11.0)
  • Installing pycodestyle (2.8.0)
  • Installing pyflakes (2.4.0)
  • Installing pygments (2.12.0)
  • Installing stack-data (0.2.0)
  • Installing tomli (2.0.1)
  • Installing typing-extensions (4.2.0)
  • Installing urllib3 (1.26.9)
  • Installing black (22.3.0)
  • Installing flake8 (4.0.1)
  • Installing ipython (8.4.0)
  • Installing pytest (7.1.2)
  • Installing requests (2.27.1)

Installing the current project: crossrefapi (1.6.0)
(.venv) fabiobatalha:~/Work/crossrefapi$ python
Python 3.8.10 (default, Mar 15 2022, 12:22:08) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import crossref
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/fabiobatalha/Work/crossrefapi/src/crossref/__init__.py", line 3, in <module>
    VERSION = pkg_resources.get_distribution("crossref").version
  File "/home/fabiobatalha/Work/crossrefapi/.venv/lib/python3.8/site-packages/pkg_resources/__init__.py", line 478, in get_distribution
    dist = get_provider(dist)
  File "/home/fabiobatalha/Work/crossrefapi/.venv/lib/python3.8/site-packages/pkg_resources/__init__.py", line 354, in get_provider
    return working_set.find(moduleOrReq) or require(str(moduleOrReq))[0]
  File "/home/fabiobatalha/Work/crossrefapi/.venv/lib/python3.8/site-packages/pkg_resources/__init__.py", line 909, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/home/fabiobatalha/Work/crossrefapi/.venv/lib/python3.8/site-packages/pkg_resources/__init__.py", line 795, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'crossref' distribution was not found and is required by the application
>>> 

@abn
Copy link
Member

abn commented Jun 1, 2022

VERSION = pkg_resources.get_distribution("crossref").version

You should try using importlib.metadata instead of pkg_resources. You can see here for an example.

That said I suspect the issue was fixed in #5703. You can try using the version of poetry from master.

You can workaround this issue by moving the version discovery into it's own module rather than __init__.py.

@finswimmer
Copy link
Member

VERSION = pkg_resources.get_distribution("crossref").version

Your distribution is still crossrefapi, even if it contains a package crossref. That's one of the reasons, why the distribution name shouldn't differ from the actual package name.

@abn
Copy link
Member

abn commented Jun 1, 2022

oh yeah; @finswimmer is right - missed that.

Just change this.

- VERSION = pkg_resources.get_distribution("crossref").version
+ VERSION = pkg_resources.get_distribution("crossrefapi").version

@fabiobatalha
Copy link
Author

fabiobatalha commented Jun 1, 2022

thanks @abn and @GenosW it works with importlib.metadata. The error was in the package name while trying to retrieve the package version.

It works also with pkg_resources.

Copy link

github-actions bot commented Mar 2, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants