purcell / emacs.d

An Emacs configuration bundle with batteries included
BSD 2-Clause "Simplified" License
6.84k stars 2.05k forks source link

python virtual environment activation #774

Closed zhangfelix closed 3 years ago

zhangfelix commented 3 years ago

I'm working in a mac(apple m1). I used M-x pythonic-activate RET /path/to/virtualenv RET to change the virtual environment in anaconda-mode. But Python Shell, pdb and flycheck use three different environments.

It works fine when I used C-c C-p, in a Python Shell.

>>> import sys
>>> print(sys.path)
['', '/Users/felix/miniforge3/envs/gluon/lib/python38.zip', '/Users/felix/miniforge3/envs/gluon/lib/python3.8', '/Users/felix/miniforge3/envs/gluon/lib/python3.8/lib-dynload', '/Users/felix/miniforge3/envs/gluon/lib/python3.8/site-packages']

But the pdb(python -m pdb test.py) use the python2.7 from the system.

 ['', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Python/2.7/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC']

While the flycheck use python from homebrew.

Syntax checkers for buffer test.py in python-mode:

First checker to run:

  python-pycompile
    - may enable:    yes
    - executable:    Found at /opt/homebrew/bin/python3
    - next checkers: python-mypy

Checkers that are compatible with this mode, but will not run until properly configured:

  python-flake8 (automatically disabled) reset
    - may enable:         no
    - executable:         Found at /opt/homebrew/bin/python3
    - configuration file: Not found
    - `flake8' module:    Missing; sys.path is ['/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python39.zip', '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9', '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload', '/opt/homebrew/lib/python3.9/site-packages']
    - next checkers:      python-pylint, python-mypy

  python-pylint (automatically disabled) reset
    - may enable:         no
    - executable:         Found at /opt/homebrew/bin/python3
    - configuration file: Not found
    - `pylint' module:    Missing; sys.path is ['/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python39.zip', '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9', '/opt/homebrew/Cellar/python@3.9/3.9.2_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/lib-dynload', '/opt/homebrew/lib/python3.9/site-packages']
    - next checkers:      python-mypy

  python-pyright (automatically disabled) reset
    - may enable: no
    - executable: Not found

  python-mypy (automatically disabled) reset
    - may enable:         no
    - may run:            t
    - executable:         Not found
    - configuration file: Not found

Flycheck Mode is enabled.  Use C-u C-c ! x to enable disabled
checkers.

--------------------

Flycheck version: 32snapshot (package: 20201228.2104)
Emacs version:    27.1
System:           aarch64-apple-darwin20.2.0
Window system:    ns
zhangfelix commented 3 years ago

I tried pyvenv, and it works. I add (require-package 'pyvenv) into init-local.el. Use M-x pyvenv-activate to specify the virtual environment directory. After that, Python Shell, pdb and flycheck use the same environment.

Maybe It is batter to add pyvenv into default configuration.

purcell commented 3 years ago

Thanks for filing this. I don't think I want to prescribe how people should manage their python virtual envs, and locally I use poetry, nix and direnv instead of pyvenv etc.

zhangfelix commented 3 years ago

You're right, this is more like a special use case. Just record it in case others encounter the same problem.

purcell commented 3 years ago

I've updated the note in init-python.el at least. :-)