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

ModuleNotFoundError: No module named 'pkg_resources' #1674

Closed
bradprob opened this issue Sep 27, 2019 · 26 comments
Closed

ModuleNotFoundError: No module named 'pkg_resources' #1674

bradprob opened this issue Sep 27, 2019 · 26 comments

Comments

@bradprob
Copy link

bradprob commented Sep 27, 2019

Summary

Elpy produces the following error when navigating through the source code:

Traceback (most recent call last):
  File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/user/.emacs.d/elpa/elpy-20190925.2241/elpy/__main__.py", line 16, in <module>
    from elpy.server import ElpyRPCServer
  File "/home/user/.emacs.d/elpa/elpy-20190925.2241/elpy/server.py", line 16, in <module>
    from elpy.blackutil import fix_code as fix_code_with_black
  File "/home/user/.emacs.d/elpa/elpy-20190925.2241/elpy/blackutil.py", line 6, in <module>
    from pkg_resources import parse_version
ModuleNotFoundError: No module named 'pkg_resources'

Config:

(use-package elpy
  :ensure t
	:init
  (elpy-enable)
  :config
  (setq elpy-rpc-python-command "python3")
  (setq python-shell-interpreter "python3")
  (setenv "WORKON_HOME" (expand-file-name "PATH_OF_PYTHON_PROJECTS")))
@galaunay
Copy link
Collaborator

Thanks for reporting,

It looks like a missing dependency.
I will fix that,in the meantime, a workaround would be to install setuptools from pip.

@galaunay
Copy link
Collaborator

Should be fixed if you update to the last Elpy version.

Please re-open this issue if the problem still exist.

@bradprob
Copy link
Author

Should be fixed if you update to the last Elpy version.

Please re-open this issue if the problem still exist.

The problem still exists:

Traceback (most recent call last):
  File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/user/.emacs.d/elpa/elpy-20190927.2050/elpy/__main__.py", line 16, in <module>
    from elpy.server import ElpyRPCServer
  File "/home/user/.emacs.d/elpa/elpy-20190927.2050/elpy/server.py", line 16, in <module>
    from elpy.blackutil import fix_code as fix_code_with_black
  File "/home/user/.emacs.d/elpa/elpy-20190927.2050/elpy/blackutil.py", line 6, in <module>
    from pkg_resources import parse_version
ModuleNotFoundError: No module named 'pkg_resources'

setuptools for python3 is also installed system wide.

@galaunay
Copy link
Collaborator

Hum, that is strange,

pkg_resources is supposed to be shipped with setuptools.

I found this post on stackoverflow mentionning the same issue.
Would it be fixed by installing python-setuptools (if it is on your distribution repositories) or re-installing it with pip install --reinstall setuptools ?

@bradprob
Copy link
Author

bradprob commented Sep 28, 2019

I install my python packages system-wide, with apt install
I have already install python3-setuptools.
I have discovered that the problem can be solved when I take the following steps:
1- remove (setq elpy-rpc-python-command "python3") from

(use-package elpy
 :ensure t
   :init
 (elpy-enable)
 :config
 (setq elpy-rpc-python-command "python3")
 (setq python-shell-interpreter "python3")
 (setenv "WORKON_HOME" (expand-file-name "PATH_OF_PYTHON_PROJECTS")))

2- restart the emacs
3- activate the python environment for a project
4- open a python source code of the project, at this point I receive the following question:
`Automatically install RPC dependencies from PyPI(needed form completion, autoformatting and documentation)?
5- choose yes, then the error does not appear

Hopefully this helps to identify the bug!

@galaunay
Copy link
Collaborator

Thanks for the feedback.

I tried to play with elpy-rpc-python-command but couldn't reproduce the issue.
Does it happen if you put (setq elpy-rpc-python-command "python3") back ?
It could have been a damaged RPC virtualenv (for some reason...).

@bradprob
Copy link
Author

bradprob commented Oct 1, 2019

Does it happen if you put (setq elpy-rpc-python-command "python3") back ?

No, putting the line back to the config file does not cause the error.

@galaunay
Copy link
Collaborator

galaunay commented Oct 1, 2019

Ok, thanks,
So maybe a somehow damaged virtualenv...
I don't know how Elpy could do something that damage a virtualenv, but please let me know if it happens again,

@galaunay galaunay closed this as completed Oct 1, 2019
@rirze
Copy link

rirze commented Oct 16, 2019

I have this same issue, installed my emacs config on a new machine and I can't get elpy is work.

I keep getting this error while working in Python files;

Traceback (most recent call last):
  File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/chronos/.emacs.d/elpa/elpy-20191011.1130/elpy/__main__.py", line 16, in <module>
    from elpy.server import ElpyRPCServer
  File "/home/chronos/.emacs.d/elpa/elpy-20191011.1130/elpy/server.py", line 16, in <module>
    from elpy.blackutil import fix_code as fix_code_with_black
  File "/home/chronos/.emacs.d/elpa/elpy-20191011.1130/elpy/blackutil.py", line 6, in <module>
    from pkg_resources import parse_version
ModuleNotFoundError: No module named 'pkg_resources'

It hiccups on every other keystroke, which is debilitating.

My elpy config is:

(use-package elpy
  :custom
  (elpy-rpc-python-command "python3")
  (elpy-modules '(elpy-module-company))
  (python-shell-interpreter "ipython3")
  (python-shell-interpreter "-i --simple-prompt")
  :config
  (elpy-enable))

@galaunay
Copy link
Collaborator

The pkg_resources is only used for black autoformatting.
I will make sure it does not hamper the use of Elpy when not present.

I found a bug that I am trying to fix in PR #1699.
It should fix your problem, (you will just need to run M-x elpy-rpc-reinstall-virtualenv) to refresh the RPC venv).

@galaunay galaunay reopened this Oct 17, 2019
@rirze
Copy link

rirze commented Oct 18, 2019

Great! Installing the latest source seems to work fine. I do run into a separate error upon choosing to install additional packages while running M-x elpy-rpc-reinstall-virtualenv:

/home/rirze/.emacs.d/elpy/rpc-venv/bin/python3: No module named pip

For now, I've enabled site-wide packages in the virtual environment, but any clarification on this would be appreciated. This environment is a newly-setup Ubuntu 18.04 with the configuration I've given above.

@galaunay
Copy link
Collaborator

That's unexpected... pip is supposed to be installed in the rpc venv.
Could you post the result from elpy-config ?

@rirze
Copy link

rirze commented Oct 24, 2019

Sure, with the site-wide packages enabled, I have:

Elpy Configuration

Emacs.............: 26.3
Elpy..............: Not found (Python), 1.31.0 (Emacs Lisp)
Virtualenv........: None
Interactive Python: ipython3 5.5.0 (/usr/bin/ipython3)
RPC virtualenv....: rpc-venv (/home/chronos/.emacs.d/elpy/rpc-venv)
 Python...........: python3 3.6.8 (/home/chronos/.emacs.d/elpy/rpc-venv/bin/python3)
 Jedi.............: 0.15.1
 Rope.............: Not found (0.14.0 available)
 Autopep8.........: 1.4.4
 Yapf.............: 0.28.0
 Black............: 19.3b0
Syntax checker....: flake8 (/usr/local/bin/flake8)

You have not activated a virtual env. While Elpy supports this, it is
often a good idea to work inside a virtual env. You can use M-x
pyvenv-activate or M-x pyvenv-workon to activate a virtual env.

The Python interpreter could not find the elpy module. Please report
to: https://github.com/jorgenschaefer/elpy/issues/new.

Without that option I have

Elpy Configuration

Emacs.............: 26.3
Elpy..............: Not found (Python), 1.31.0 (Emacs Lisp)
Virtualenv........: None
Interactive Python: ipython3 5.5.0 (/usr/bin/ipython3)
RPC virtualenv....: rpc-venv (/home/chronos/.emacs.d/elpy/rpc-venv)
 Python...........: python3 3.6.8 (/home/chronos/.emacs.d/elpy/rpc-venv/bin/python3)
 Jedi.............: Not found (0.15.1 available)
 Rope.............: Not found (0.14.0 available)
 Autopep8.........: Not found (1.4.4 available)
 Yapf.............: Not found (0.28.0 available)
 Black............: Not found (19.3b0 available)
Syntax checker....: flake8 (/usr/local/bin/flake8)

You have not activated a virtual env. While Elpy supports this, it is
often a good idea to work inside a virtual env. You can use M-x
pyvenv-activate or M-x pyvenv-workon to activate a virtual env.

The Python interpreter could not find the elpy module. Please report
to: https://github.com/jorgenschaefer/elpy/issues/new.

The jedi package is not available. Completion and code navigation will
not work.

[Install jedi]

The autopep8 package is not available. Commands using this will not
work.

[Install autopep8]

The yapf package is not available. Commands using this will not work.

[Install yapf]

The black package is not available. Commands using this will not work.

[Install black]

Again, this is a relatively clean installation, maybe it could be reproduced on a new VM for example? Hope this helps.

@galaunay
Copy link
Collaborator

I tried from a clean Emacs installation without success.

I suspect a problem with the way Elpy is creating the virtualenv.
Elpy should be using something like that to create the virtualenv on your system:

python3 -m venv /home/chronos/.emacs.d/elpy/rpc-venv

What happen if you delete the /home/chronos/.emacs.d/elpy/rpc-venv and recreate it using the above command ? Is there pip binaries under /home/chronos/.emacs.d/elpy/rpc-venv/bin/ as expected ?

@rirze
Copy link

rirze commented Oct 24, 2019

I tried removing the folder and reinstalling, no success. Under rpc-venv/bin, there's only python and python3, which both link the system binaries anyways. Let me know if I can try anything else to help out.

@galaunay
Copy link
Collaborator

I checked the venv package documentation again (here), and it mentions that venv should install pip by default:

Unless the --without-pip option is given, ensurepip will be invoked to bootstrap pip into the virtual environment.

So I am a bit confused why it is not doing it.

What happen if you run python -m ensurepip -vvv from inside the virtualenv ?
Does it add the pip binaries in /home/chronos/.emacs.d/elpy/rpc-venv/bin as it should ?

@rirze
Copy link

rirze commented Oct 26, 2019

Well, that's the thing, there's no way elpy is creating the virtual environment fully. I can't even activate the virtual environment outside of emacs-- the directory is under-formed:

chronos@pc:~/.emacs.d/elpy$ ls -R rpc-venv/
rpc-venv/:
bin  elpy-rpc-python-path-command  include  lib  lib64  pyvenv.cfg

rpc-venv/bin:
python  python3

rpc-venv/include:

rpc-venv/lib:
python3.6

rpc-venv/lib/python3.6:
site-packages

rpc-venv/lib/python3.6/site-packages:

AFAIK, a normal virtual environment would have the activate command under the bin folder.

@galaunay
Copy link
Collaborator

So if the virtualenv is not created properly you may find some useful information in the " *venv*" (or " *virtualenv*") buffer Elpy creates when using elpy-rpc-reinstall-virtualenv.

If nothing useful there, maybe the problem comes from the command Elpy uses to create the virtualenv:

python3 -m venv /home/chronos/.emacs.d/elpy/rpc-venv

Does it creates a viable virtualenv is you run that from the command line ?

@rirze
Copy link

rirze commented Oct 29, 2019

Aha, I think you've found it:

chronos@pc:~$ python3 -m venv /home/chronos/.emacs.d/elpy/rpc-venv
The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.

    apt-get install python3-venv

You may need to use sudo with that command.  After installing the python3-venv
package, recreate your virtual environment.

Failing command: ['/home/chronos/.emacs.d/elpy/rpc-venv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']

After running sudo apt install python3-venv, it created the virtual environment successfully. Going back to emacs, running M-x elpy-rpc-reinstall-virtualenv worked seamlessly and installed the additional libraries as well.

Now running M-x elpy-config shows this:

Elpy Configuration

Emacs.............: 26.3
Elpy..............: Not found (Python), 1.31.0 (Emacs Lisp)
Virtualenv........: None
Interactive Python: ipython3 5.5.0 (/usr/bin/ipython3)
RPC virtualenv....: rpc-venv (/home/chronos/.emacs.d/elpy/rpc-venv)
 Python...........: python3 3.6.8 (/home/chronos/.emacs.d/elpy/rpc-venv/bin/python3)
 Jedi.............: 0.15.1
 Rope.............: 0.14.0
 Autopep8.........: 1.4.4
 Yapf.............: 0.28.0
 Black............: 19.10b0
Syntax checker....: flake8 (/usr/local/bin/flake8)

You have not activated a virtual env. While Elpy supports this, it is
often a good idea to work inside a virtual env. You can use M-x
pyvenv-activate or M-x pyvenv-workon to activate a virtual env.

The Python interpreter could not find the elpy module. Please report
to: https://github.com/jorgenschaefer/elpy/issues/new.

Which is great! I now only have the issue where the elpy module is not found, but I think that warrants a different issue rather than discussing it in this thread. Thanks again!

@galaunay
Copy link
Collaborator

Good to know it works now.
I made some modifications so that users in the same configuration will be properly warned in the future.

Did you by any chance installed Elpy from the Debian package repository ?
(If so, we may need to add python-venv as a dependency)

I now only have the issue where the elpy module is not found, but I think that warrants a different issue rather than discussing it in this thread. Thanks again!

Please open a new one if it is giving you headaches :).

@rirze
Copy link

rirze commented Oct 30, 2019

For this setup, I have it installed using use-package, so it installed from melpa.

@nickurbanik
Copy link

This problem recurs with Fedora 39. Workaround is to activate the venv in bash, and pip install setuptools.

@finjulhich
Copy link

finjulhich commented Dec 17, 2023

@nickurbanik @galaunay
emacs 29.1 Fedora 39

>>>> pip list | grep -Ei 'virtualenv|setuptools'
setuptools                    67.7.2
virtualenv                    20.25.0

elpy version 20230803.1455

"activate the venv in bash" I don't understand, how do you mean?

Elpy output still shows
`Traceback (most recent call last):
File "", line 198, in _run_module_as_main

File "", line 88, in _run_code

File "~/.emacs.d/elpa/elpy-20230803.1455/elpy/main.py", line 16, in

from elpy.server import ElpyRPCServer

File "~/.emacs.d/elpa/elpy-20230803.1455/elpy/server.py", line 16, in

from elpy.blackutil import fix_code as fix_code_with_black

File "~/.emacs.d/elpa/elpy-20230803.1455/elpy/blackutil.py", line 33, in

current_version = parse_version(black.__version__)

                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "~/.emacs.d/elpa/elpy-20230803.1455/elpy/blackutil.py", line 14, in parse_version

raise Fault("`pkg_resources` could not be imported, "

elpy.rpc.Fault: pkg_resources could not be imported, please reinstall Elpy RPC virtualenv with M-x elpy-rpc-reinstall-virtualenv
`

@berhoel
Copy link
Contributor

berhoel commented Dec 17, 2023

distutils is deleted from python as of 3.12.

Maybe packaging.version.Version can be used instead of pkg_resources.parse_version?

>>> from packaging.version import Version
>>> import black
>>> Version(black.__version__)>Version("21.5b1")
True
>>> black.__version__
'23.12.0'

packaging is already required by black:

> pip show packaging
Name: packaging
Version: 23.2
Summary: Core utilities for Python packages
Home-page: 
Author: 
Author-email: Donald Stufft <[email protected]>
License: 
Location: /home/hoel/.pyenv/versions/3.12.0/lib/python3.12/site-packages
Requires: 
Required-by: black, matplotlib, pyproject-api, pytest, Sphinx, webdriver-manager

@finjulhich
Copy link

The above has worked thank you very much

@berhoel
Copy link
Contributor

berhoel commented Dec 17, 2023

I created a pull request (#2037) to replace pkg_resources.parse_version by packaging.version.Version.

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

No branches or pull requests

6 participants