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

virtual environment #1707

Closed
nc10 opened this issue Oct 27, 2019 · 4 comments
Closed

virtual environment #1707

nc10 opened this issue Oct 27, 2019 · 4 comments

Comments

@nc10
Copy link

nc10 commented Oct 27, 2019

Summary

Virtual environment is not activated in Emacs

Steps to reproduce

My configuration

I have a working virtual environment created by
$ virtualenv py3env
in ~\pythonVirtualEnv folder. I can activated the environment in bash via source /bin/activate.

I have the following in a my emacs initialization file
(use-package elpy
:ensure t
:defer t
:init
(advice-add 'python-mode :before 'elpy-enable))

(pyvenv-activate "/home/naveed/pythonVirtualEnv/py3env")
(setq python-shell-interpreter "jupyter"
      python-shell-interpreter-args "console --simple-prompt"
      python-shell-prompt-detect-failure-warning nil)
(add-to-list 'python-shell-completion-native-disabled-interpreters
             "jupyter")

In a fresh emacs instance which runs the above code at its initialization step, when I run
$ echo $PATH in
eshell I do not see the path of my virtual environment in my $PATH. When I run
(pyvenv-activate "/home/naveed/pythonVirtualEnv/py3env")
in my scratch buffer again and get the echo of $PATH, I don't see any changes.
When I run pyvenv-activate interactively, I see "/home/naveed/pythonVirtualEnv/py3env//bin" is added to $PATH (note the two / before bin). Same observation applies to exec-path variable.
My main problem is that the instance of python invoked in emacs is not the one specified by the virtual environment. By reading some of the discussions on this page, I believe the problem is with my path variables. I would appreciate your help.

OS

macOS Mojave
emacs: 26.3 (9.0) [installed via brew]

Result of (elpy-config)

Emacs.............: 26.3
Elpy..............: 1.31.0
Virtualenv........: py3env (/home/naveed/pythonVirtualEnv/py3env)
Interactive Python: jupyter (not found)
RPC virtualenv....: elpy-rpc-venv (/Users/naveed/.virtualenvs/elpy-rpc-venv)
Python...........: python 2.7.10 (/Users/naveed/.virtualenvs/elpy-rpc-venv/bin/python)
Jedi.............: 0.15.1
Rope.............: 0.14.0
Autopep8.........: 1.4.4
Yapf.............: 0.28.0
Black............: Not found (19.3b0 available)
Syntax checker....: Not found (flake8)

Elpy configuration in my init.el

(use-package elpy
:ensure t
:defer t
:init
(advice-add 'python-mode :before 'elpy-enable))

(pyvenv-activate "/home/naveed/pythonVirtualEnv/py3env")
(setq python-shell-interpreter "jupyter"
      python-shell-interpreter-args "console --simple-prompt"
      python-shell-prompt-detect-failure-warning nil)
(add-to-list 'python-shell-completion-native-disabled-interpreters
             "jupyter")
@galaunay
Copy link
Collaborator

When I run pyvenv-activate interactively, I see "/home/naveed/pythonVirtualEnv/py3env//bin" is added to $PATH (note the two / before bin).

That is fine.

In a fresh emacs instance which runs the above code at its initialization step, when I run
$ echo $PATH in
eshell I do not see the path of my virtual environment in my $PATH. When I run
(pyvenv-activate "/home/naveed/pythonVirtualEnv/py3env")
in my scratch buffer again and get the echo of $PATH, I don't see any changes.

pyvenv-activate fails without warnings.
So if there is a typo in the path you are giving it, it will just do nothing without complaining.
Are you sure there is no typo in the path you are using in your init file ?

My main problem is that the instance of python invoked in emacs is not the one specified by the virtual environment. By reading some of the discussions on this page, I believe the problem is with my path variables. I would appreciate your help.

elpy-config seems to indicate that your virtualenv is properly activated, but that jupyter cannot be found anywhere in your PATH. Is jupyter supposed to be installed in your virtualenv or system-wide ?

@nc10
Copy link
Author

nc10 commented Oct 30, 2019

Hello,

Yes, you are right. There was a typo in the path. With this fixed, elpy still chooses python 2.7.10 as the python executable. I have all packages installed in py3env virtual environment (including black and jupyter). Here is the output of elpy-config. Any thoughts?

Elpy Configuration

Emacs.............: 26.3
Elpy..............: 1.31.0
Virtualenv........: py3env (/Users/naveed/pythonVirtualEnv/py3env)
Interactive Python: jupyter 4.6.1 (/Users/naveed/pythonVirtualEnv/py3env/bin/jupyter)
RPC virtualenv....: elpy-rpc-venv (/Users/naveed/.virtualenvs/elpy-rpc-venv)
Python...........: python 2.7.10 (/Users/naveed/.virtualenvs/elpy-rpc-venv/bin/python)
Jedi.............: 0.15.1
Rope.............: 0.14.0
Autopep8.........: 1.4.4
Yapf.............: 0.28.0
Black............: Not found (19.10b0 available)
Syntax checker....: flake8 (/Users/naveed/pythonVirtualEnv/py3env/bin/flake8)

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

@galaunay
Copy link
Collaborator

Elpy is using a dedicated virtualenv (/Users/naveed/.virtualenvs/elpy-rpc-venv in your case) to install the python dependencies (jedi, rope, ...) without disturbing the environment you are working in.

If you want the RPC to use python3, you will need to use (setq elpy-rpc-python-command "python3") (it is set to "python" by default, which seems to be python2 in your case).
Black will be installed automatically.

Everything should work fine, but if you really want to use the dependencies installed in your virtualenv (py3env), you can set (setq elpy-rpc-virtualenv-path 'current).
The RPC (responsible for getting completion, getting documentation and autofixing code) will be started in your py3env environment.

@nc10
Copy link
Author

nc10 commented Jan 12, 2020

Thanks!

@nc10 nc10 closed this as completed Jan 12, 2020
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

2 participants