0

I am trying to setup company-jedi to autocomplete for python3. The autocomplete is working for standard python3 modules; however, it is not working for packages I installed using pip3, in this case numpy.

The virtualenv package required by company-jedi has been installed using:

sudo pip3 install virtualenv

Python3 has been installed using:

brew install python3

Numpy was installed using the following command:

sudo pip3 install numpy

I have followed the instruction from this link but to no avail. If I install numpy using pip instead of pip3 auto-complete works; therefore, I think that jedi is still using python2 instead of python3. The command M-x jedi:install-server does not return any errors.

The path of python3 was determined using the command which python3. The exec-path-from-shell package is used to make sure the environment variables on emacs match those in the shell. Without this package the function M-x jedi:install-server returns an error that virtualenv does not exist.

My question is, how can I setup jedi to use python3 for auto-completion instead of python2?


Init.el

;; To get path from shell
(use-package exec-path-from-shell
  :ensure t
  :config
  (when (memq window-system '(mac ns x))
    (exec-path-from-shell-initialize)))

;; Installing company mode
(use-package company
  :ensure t
  :config
  (add-hook 'after-init-hook 'global-company-mode))

;; Disable the delay
(setq company-idle-delay 0)

(eval-after-load 'company
  '(progn
     (define-key company-active-map (kbd "TAB") 'company-complete-common-or-cycle)
     (define-key company-active-map (kbd "<tab>") 'company-complete-common-or-cycle)
     (define-key company-active-map (kbd "S-TAB") 'company-select-previous)
     (define-key company-active-map (kbd "<backtab>") 'company-select-previous)))

(use-package company-jedi
  :ensure t)

(defun my/python-mode-hook ()
  (add-to-list 'company-backends 'company-jedi))
(add-hook 'python-mode-hook 'my/python-mode-hook)

;; Python
(setq python-shell-interpreter "python3")

(setq jedi:environment-root "jedi")  ; or any other name you like
(setq jedi:environment-virtualenv
      (append python-environment-virtualenv
              '("--python" "/usr/local/bin/python3")))

I am currently on MacOs version: 10.13.6

  • I believe that the reason jedi wants you to have virtual environment is because it expects you to install your stuff into a virtual environment, where it will look for source to autocomplete from. To do this, first call virtualenv -p $(which python3) .venv, then .venv/bin/activate, then, in Emacs, M-x pyvenv-activate .venv RET. Install the packages you need with the pip installed in virtual environment, including jedi. – wvxvw Aug 2 '18 at 5:32
  • It did not work. I installed the pyvenv package and saved .venv in the home directory. Do I need to do something in addition to those? – Noel Farrugia Aug 2 '18 at 6:59
  • Wait, pyvenv the Emacs Lisp package for working with Python mode in Emacs, or pyvenv, the Python script that used to be distributed with Python distribution from python.org? At any rate, this is not how you are supposed to work with virtual environments. They are meant to be saved together with the project your are working on, or, they can be managed by something like virtualenvwrapper script. See this guide for details: python-guide-cn.readthedocs.io/en/latest/dev/virtualenvs.html . – wvxvw Aug 2 '18 at 8:36
  • I was referring to the pyvenv Emacs package. What I can't understand is how creating a new virtual environment is going to help with the autocompletion? Because the jedi plugin already creates a new virtual environment and stores it in the ~/.emacs.d directory – Noel Farrugia Aug 2 '18 at 9:24
  • Well, the fact that it creates a virtual environment is strange... but maybe it needs it for something else. You need virtual environments in general, not only for Jedi, and the reason is that you cannot have multiple versions of the same Python package installed at the same time. So, virtual environment, kind of, helps to solve this problem. I would expect anything that does code completion to rely on you to put packages you want completion for to live in a specific environment. – wvxvw Aug 2 '18 at 10:30
1

Are you using Python 3.7? Jedi doesn't support that version yet and so falls back to the next highest, which on most systems should be 2.7.

There's a couple of things you can do: You can simply wait for Jedi to support 3.7 which shouldn't take too long. You could also downgrade to 3.6. Or you could manually add 3.7 to the supported versions in the Jedi code. Supported versions are defined by the variable _SUPPORTED_PYTHONS in the file jedi/api/environment.py within the Jedi package. Since you probably shouldn't mess around with globally installed packages, it might be best to first copy the package to your virtualenv (something like cp -r /usr/lib/python3.7/site-packages/jedi ~/.emacs.d/.python-environments/default/lib/python3.7/site-packages/) and then edit the file there.

Since Python 3.7 isn't officially supported, this last way might cause some issues with using Jedi in Emacs. Also, since the package in the virtualenv overrides the global one, don't forget to remove it once actual 3.7 support arrives in Jedi.

  • That's probably it then. Thank you so much for your time! – Noel Farrugia Aug 22 '18 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.