Python Forum
Emacs 28 and venv - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: Emacs 28 and venv (/thread-38837.html)



Emacs 28 and venv - pisterpesto - Nov-29-2022

I am hoping for a fellow Emacs user being here.

I would be very happy if something could explain to me dump terms how I get Emacs to be aware a standard Python venv using the default python-mode python.el.
Not third-party tools like pyenv, anaconda etc.

I found this explanation in python.el

Quote:Shell virtualenv support: The shell also contains support for
virtualenvs and other special environment modifications thanks to
python-shell-process-environment and python-shell-exec-path.
These two variables allows you to modify execution paths and
environment variables to make easy for you to setup virtualenv rules
or behavior modifications when running shells. Here is an example
of how to make shell processes to be run using the /path/to/env/
virtualenv:

(setq python-shell-process-environment
      (list
       (format "PATH=%s" (mapconcat
                          'identity
                          (reverse
                           (cons (getenv "PATH")
                                 '("/path/to/env/bin/")))
                          ":"))
       "VIRTUAL_ENV=/path/to/env/"))
(python-shell-exec-path . ("/path/to/env/bin/"))
Since the above is cumbersome and can be programmatically
calculated, the variable `python-shell-virtualenv-root' is
provided. When this variable is set with the path of the
virtualenv to use, process-environment' and exec-path` get proper
values in order to run shells inside the specified virtualenv. So
the following will achieve the same as the previous example:

(setq python-shell-virtualenv-root "/path/to/env/")
Also the python-shell-extra-pythonpaths variable have been
introduced as simple way of adding paths to the PYTHONPATH without
affecting existing values.

Shell package support: you can enable a package in the current
shell so that relative imports work properly using the
python-shell-package-enable command.

I have lots of small "projects" all over the place I just created a directory and executedpython3 -m venv venv.
I cannot import and modules from my venv in Emacs, even my I open a shell and activate the environment.

And hardcoding python-shell-virtualenv-root in my init file for every project I have and for future projects. Doesn't seem right, that can't be what it is for.

So how do one use venv in Emacs?


RE: Emacs 28 and venv - Larz60+ - Nov-30-2022

something like this: https://github.com/jorgenschaefer/elpy ?