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

jedi ignores current virtualenv? #178

Closed
psihonavt opened this issue Oct 17, 2014 · 13 comments
Closed

jedi ignores current virtualenv? #178

psihonavt opened this issue Oct 17, 2014 · 13 comments

Comments

@psihonavt
Copy link

system information:
jedi:show-setup-info

;; Emacs Lisp version:
(:emacs-version "24.3.93.1" :jedi-version "0.2.0alpha2" :python-environment-version "0.0.2alpha0")
;; Python version:
((:version "2.7.4 (default, Sep 26 2013, 03:20:26) \n[GCC 4.7.3]" :name "sys" :file nil)
 (:version "0.8.1-final0" :name "jedi" :file "/home/akoval/.emacs.d/.python-environments/default/local/lib/python2.7/site-packages/jedi/__init__.pyc")
 (:version "0.0.5" :name "epc" :file "/home/akoval/.emacs.d/.python-environments/default/local/lib/python2.7/site-packages/epc/__init__.pyc")
 (:version "0.0.3" :name "sexpdata" :file "/home/akoval/.emacs.d/.python-environments/default/local/lib/python2.7/site-packages/sexpdata.pyc"))
;; Command line:
(:virtualenv "/usr/local/bin/virtualenv" :virtualenv-version "1.11.6\n")
;; Customization:
((jedi:complete-on-dot . t)
 (jedi:doc-display-buffer . display-buffer)
 (jedi:doc-hook view-mode)
 (jedi:doc-mode . rst-mode)
 (jedi:environment-root)
 (jedi:environment-virtualenv)
 (jedi:get-in-function-call-delay . 1000)
 (jedi:get-in-function-call-timeout . 3000)
 (jedi:goto-definition-config
  (nil nil nil)
  (t nil nil)
  (nil definition nil)
  (t definition nil)
  (nil nil t)
  (t nil t)
  (nil definition t)
  (t definition t))
 (jedi:goto-definition-marker-ring-length . 16)
 (jedi:imenu-create-index-function . jedi:create-nested-imenu-index)
 (jedi:import-python-el-settings . t)
 (jedi:install-imenu)
 (jedi:install-python-jedi-dev-command "pip" "install" "--upgrade" "git+https://github.com/davidhalter/jedi.git@dev#egg=jedi")
 (jedi:key-complete .
                    [C-tab])
 (jedi:key-goto-definition .
                           [67108910])
 (jedi:key-goto-definition-pop-marker .
                                      [67108908])
 (jedi:key-related-names . "�r")
 (jedi:key-show-doc . "�d")
 (jedi:server-args)
 (jedi:server-command "/home/akoval/.emacs.d/.python-environments/default/bin/jediepcserver")
 (jedi:setup-keys . t)
 (jedi:tooltip-method pos-tip popup)
 (jedi:use-shortcuts)
 (python-environment-default-root-name . "default")
 (python-environment-directory . "~/.emacs.d/.python-environments")
 (python-environment-virtualenv "virtualenv" "--system-site-packages" "--quiet"))

my setup for jedi:

(provide 'jedi-setup)
;; base setup
(autoload 'jedi:setup "jedi" nil t)
(add-hook 'python-mode-hook 'jedi:setup)
;; default keyboard shortcuts
(setq jedi:setup-keys t)
;; complete on `dot`
(setq jedi:complete-on-dot t)

my scenario is next:

  1. Open some python file from one of projects (at this stage auto-compilation for default modules works fine)
  2. Perform activation of venv of this project: M-x venv-workon RET <smth> RET
  3. Auto-compilation/goto-definition of packages from this venv won't work
    Running eshell (M-x eshell) with active venv will launch session with proper VIRTUAL_ENV variable:
Welcome to the Emacs shell

wg_geo $ echo $VIRTUAL_ENV
/home/akoval/.virtualenvs/wg_geo/

Should I somehow instruct jedi to change its sys.path after activating needed environment?

@marsam
Copy link

marsam commented Oct 22, 2014

Hi:
by the command venv-workon I'll guess that you're using virtualenvwrapper.el.

jedi.el is sensitive to the variable python-shell-virtualenv-path which virtualenvwrapper.el sets globally. So you only need to set the variable python-shell-virtualenv-path to your virtualenv path.

Now, for your problem actually you need to update match the following variables.

;; Used by virtualenvwrapper.el
(setq venv-location (expand-file-name "~/.envs"))   ;; Change with the path to your virtualenvs
;; Used python-environment.el and by extend jedi.el
(setq python-environment-directory venv-location)

let me know if this helps

@psihonavt
Copy link
Author

Hi @marsam! Thanks for reply.
Yes, I'm using virtualenvwrapper. I've updated definition of python-environment-directory to point to directory with my virtualenvs. After restarting emacs I was asked to re-execute jedi:install-server (it created default venv with jedi in my folder with virtualenvs).
Executing venv-workon <venv> defines needed variables in proper way:

python-shell-virtualenv-path is a variable defined in `python.el'.
Its value is "/home/akoval/.virtualenvs/wg_geo/"
Original value was nil
--------------
python-shell-exec-path is a variable defined in `python.el'.
Its value is nil

  This variable is safe as a file local variable if its value
  satisfies the predicate `listp'.

Documentation:
List of path to search for binaries.
This variable follows the same rules as `exec-path' since it
merges with it before the process creation routines are called.
When this variable is nil, the Python shell is run with the
default `exec-path'.

You can customize this variable.
-----------
exec-path is a variable defined in `C source code'.
Its value is
("/home/akoval/.virtualenvs/wg_geo/bin" "/home/akoval/bin" "/usr/local/sbin" "/usr/local/bin" "/usr/sbin" "/usr/bin" "/sbin" "/bin" "/usr/games" "/usr/local/games" "/opt/emacs/libexec/emacs/24.3.93/x86_64-unknown-linux-gnu")

Original value was 
("/home/akoval/bin" "/usr/local/sbin" "/usr/local/bin" "/usr/sbin" "/usr/bin" "/sbin" "/bin" "/usr/games" "/usr/local/games" "/home/akoval/Downloads/emacs-24.3.93/lib-src" "/opt/emacs/libexec/emacs/24.3.93/x86_64-unknown-linux-gnu"

does it look legit? but still no luck with recognizing site-packages from this virtualenv :(

@marsam
Copy link

marsam commented Oct 22, 2014

Remeber that python-shell-virtualenv-path must be defined before the jediepcserver command is started, To check if has recognized you virtualenv: the output of the command ps -fe | grep -i jediepcserver should show --virtual-env /path/to/my/venv/, if doesn't show you need to restart the jediepcserver.

protip, you don't really need virtualenvwrapper.el, you can set the your virtualenv in your .dir-locals.el with:

((nil . ((python-shell-virtualenv-path . "/path/to/my/venv/"))))

@psihonavt
Copy link
Author

With opened emacs and activated venv, I killed via kill -9 all the jediepcservers (for some reason, M-x jedi:stop-server was unable to stop servers - it reports smth like Jedi server is already killed but in the end even more jediepcservers have appeared in processes list :) ):

  1. Navigated to some new python buffer (in order to trigger jediepcserver launch)
  2. Checked for correct venv:
akoval   27743  9725 12 18:39 pts/0    00:00:00 /home/akoval/.virtualenvs/default/bin/python /home/akoval/.virtualenvs/default/bin/jediepcserver --virtual-env /home/akoval/.virtualenvs/launcher/

And still no luck in recognizing packages from this virtualenv.

@marsam
Copy link

marsam commented Oct 22, 2014

that should work :/

  1. Calling jedi:goto-definition works for you?
  2. What do you mean when you say that doesn't recognize the packages from your virtualenv?

@psihonavt
Copy link
Author

  1. No, it returns Defenition not found
  2. I do have a lot of packages in my venv, and for some of them auto-completion/goto definition works.
    Can you suggest some ways of debugging jedi(epcserver) API?
    I've found a bit of information here: http://jedidjah.ch/code/2013/1/19/why_jedi_not_rope/ . Can I in some way (through API) point to needed virtualenv and test/debug completion?

@dct
Copy link

dct commented Apr 22, 2015

Hi, I had the same trouble as you and managed to get jedi completion to work after applying PR #223. Maybe you would also want to give it a go?

@kracekumar
Copy link
Contributor

I am also facing similar issue.

I am using venv named ws which uses python 3.4.

(ws)➜  ~  which python
/home/kracekumar/.virtualenvs/ws/bin/python
(ws)➜  ~  python --version
Python 3.4.0
Sample code
import autobahn
import collections

C-c . on collections takes me to python 2.7 codebase(/usr/lib/python2.7/), whereas on autobahn says Definition not found.

(ws)➜  ~  ps -fe | grep -i jediepcserver
kraceku+  8210  7635  0 13:23 pts/1    00:00:00 /home/kracekumar/.virtualenvs/default/bin/python /home/kracekumar/.virtualenvs/default/bin/jediepcserver
kraceku+  8214  7635  0 13:23 pts/23   00:00:00 /home/kracekumar/.virtualenvs/default/bin/python /home/kracekumar/.virtualenvs/default/bin/jediepcserver --virtual-env /home/kracekumar/.virtualenvs/ws/
kraceku+  9042  7234  0 13:34 pts/12   00:00:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=.cvs --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn -i jediepcserver

init.el setup

(require 'virtualenvwrapper)
(venv-initialize-interactive-shells) ;; if you want interactive shell support
(venv-initialize-eshell) ;; if you want eshell support
(setq venv-location "/home/kracekumar/.virtualenvs")
(setq python-environment-directory venv-location)

;; jedi setup
(add-hook 'python-mode-hook 'jedi:setup)
(setq jedi:complete-on-dot t)
(add-hook 'python-mode-hook 'jedi:ac-setup)

Jedi:show-setup

;; Emacs Lisp version:
(:emacs-version "24.3.1" :jedi-version "0.2.1" :python-environment-version "0.0.2alpha0")
;; Python version:
((:version "2.7.6 (default, Mar 22 2014, 22:59:56) \n[GCC 4.8.2]" :name "sys" :file nil)
 (:version "0.9.0" :name "jedi" :file "/home/kracekumar/.virtualenvs/default/local/lib/python2.7/site-packages/jedi/__init__.pyc")
 (:version "0.0.5" :name "epc" :file "/home/kracekumar/.virtualenvs/default/local/lib/python2.7/site-packages/epc/__init__.pyc")
 (:version "0.0.3" :name "sexpdata" :file "/home/kracekumar/.virtualenvs/default/local/lib/python2.7/site-packages/sexpdata.pyc"))
;; Command line:
(:virtualenv "/usr/local/bin/virtualenv" :virtualenv-version "1.11.1\n")
;; Customization:
((jedi:complete-on-dot . t)
 (jedi:doc-display-buffer . display-buffer)
 (jedi:doc-hook view-mode)
 (jedi:doc-mode . rst-mode)
 (jedi:environment-root)
 (jedi:environment-virtualenv)
 (jedi:get-in-function-call-delay . 1000)
 (jedi:get-in-function-call-timeout . 3000)
 (jedi:goto-definition-config
  (nil nil nil)
  (t nil nil)
  (nil definition nil)
  (t definition nil)
  (nil nil t)
  (t nil t)
  (nil definition t)
  (t definition t))
 (jedi:goto-definition-marker-ring-length . 16)
 (jedi:imenu-create-index-function . jedi:create-nested-imenu-index)
 (jedi:import-python-el-settings . t)
 (jedi:install-imenu)
 (jedi:install-python-jedi-dev-command "pip" "install" "--upgrade" "git+https://github.com/davidhalter/jedi.git@dev#egg=jedi")
 (jedi:key-complete .
                    [C-tab])
 (jedi:key-goto-definition .
                           [67108910])
 (jedi:key-goto-definition-pop-marker .
                                      [67108908])
 (jedi:key-related-names . "�r")
 (jedi:key-show-doc . "�d")
 (jedi:server-args)
 (jedi:server-command "/home/kracekumar/.virtualenvs/default/bin/jediepcserver")
 (jedi:setup-keys)
 (jedi:tooltip-method pos-tip popup)
 (jedi:use-shortcuts)
 (python-environment-default-root-name . "default")
 (python-environment-directory . "/home/kracekumar/.virtualenvs")
 (python-environment-virtualenv "virtualenv" "--system-site-packages" "--quiet"))

Python 2.7 setup

After creating a new virtualenv ws2.7 using python 2.7, setting venv-workon as ws2.7 and running jedi:start-dedicated-server with args /home/kracekumar/.virtualenvs/default/bin/jediepcserver --virtual-env /home/kracekumar/.virtualenvs/ws2.7/, go to definition on autobahn works.

Is there way to use Python 3 with jedi ?

@kracekumar
Copy link
Contributor

@dct Solution works!

@kracekumar
Copy link
Contributor

Sent a pull request #232

@syohex
Copy link
Collaborator

syohex commented May 11, 2015

I have merged #232.

@jpemberthy
Copy link

Hi. Something that worked for me without having to deal with virtualenvwrapper was to just pass the virtual env(s) to jedi server. e.g

(setq jedi:server-args
      '("--virtual-env" "SOME/VIRTUAL_ENV_1"
        "--virtual-env" "SOME/VIRTUAL_ENV_2"))

(add-hook 'python-mode-hook 'jedi:setup)

@immerrr
Copy link
Collaborator

immerrr commented Nov 12, 2018

There has been a lot of improvement regarding virtualenv handling in jedi itself, and the current master of emacs-jedi fully supports them.

venv-workon and pyvenv-workon should definitely be supported out of the box now. --virtual-env command line parameters are also supported as before. Please, update and let me know if the issue persists.

@immerrr immerrr closed this as completed Nov 12, 2018
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

7 participants