jorgenschaefer / elpy

Emacs Python Development Environment
GNU General Public License v3.0
1.89k stars 259 forks source link

Libraries in my virtual environment not recognized by elpy #1164

Open clarkeh opened 7 years ago

clarkeh commented 7 years ago

I tried running a simple python program using c-c c-c in emacs

in the first line i have import pandas as pd

When looking at my elpy config file it shows me that it recognizes my virtual enviornment. `Elpy Configuration

Virtualenv........: (/Users/chughes/emacsenv/) RPC Python........: 2.7.10 (/Users/chughes/emacsenv/bin/python) Interactive Python: python (/Users/chughes/emacsenv/bin/python) Emacs.............: 24.5.1 Elpy..............: 1.16.0 Jedi..............: Not found (0.10.2 available) Rope..............: Not found (0.10.5 available) Importmagic.......: Not found (0.1.7 available) Autopep8..........: Not found (1.3.2 available) Yapf..............: Not found (0.16.3 available) Syntax checker....: Not found (flake8)`

When i run the program i get the follwing error.

>>> import codecs, os;__pyfile = codecs.open('''/var/folders/xw/n_dmbsv54l1bwqv37dmn5r3c0009zp/T/py23156FxK''', encoding='''utf-8''');__code = __pyfile.read().encode('''utf-8''');__pyfile.close();os.remove('''/var/folders/xw/n_dmbsv54l1bwqv37dmn5r3c0009zp/T/py23156FxK''');exec(compile(__code, '''/Users/chughes/Desktop/Python Projects/Coursera Python Data Science/course1_downloads/practice2.py''', 'exec')); Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/chughes/Desktop/Python Projects/Coursera Python Data Science/course1_downloads/practice2.py", line 3, in <module> import pandas as pd ImportError: No module named pandas

any help would be greatly appreciated.

jorgenschaefer commented 7 years ago

Hello, and thanks for the report. Does this happen also when you close the interactive Python shell and start it again? If it was running from when before the virtualenv was activated, it will have the old environment still.

balbirthomas commented 3 years ago

Hi,

I have a similar problem. The problem persists even after M-x elp-rpc-restart and closing and restarting (C-c C-c) python shell. I am not sure if this a result of my misreading the documentation and I am missing steps to make elpy work with virtual environments. Here are the steps to reproduce.

  1. mkdir ~/scratch && cd ~/scratch
  2. python3 -m venv .
  3. Install some module (that is not installed on host system), after activating (source bin/activate) say pip3 install pyyaml
  4. Start emacs and open a file say src.py in ~/scratch directory.
  5. Activate the created virtual environment within emacs with M-x pyvenv-activate responding with ~/scratch on prompt.
  6. C-c C-c in Emacs now brings up a python shell that can not import the installed module (yaml) even though the package is installed in ~/scratch/lib/python3.7/site-packages. Within the python shell sys.path only shows ~/scratch among other host system paths.

Output of M-x elpy-config

Virtualenv........: scratch (/home/bt/scratch)
RPC Python........: 3.7.3 (/usr/bin/python3)
Interactive Python: /usr/bin/python3 (/usr/bin/python3)
Emacs.............: 26.1
Elpy..............: 1.28.0
Jedi..............: 0.13.2 (0.17.2 available)
Rope..............: Not found
Autopep8..........: 1.4.3
Yapf..............: Not found
Black.............: Not found
Syntax checker....: flake8 (/usr/bin/flake8)

The python interactive interpreter (/usr/bin/python3) is not installed
on the current virtualenv (/home/bt/scratch/). The system binary
(/usr/bin/python3) will be used instead.

Emacs config

;; Enable elpy
(advice-add 'python-mode :before 'elpy-enable)
;; elpy options
(setq elpy-shell-echo-input nil)
;; Set the default python interpreter
(setq python-shell-interpreter "python3")

Elpy Versions used to reproduce this problem : 1.32.0 and 1.28.0. Emacs versions used : 26.3 and 26.1 Platforms used: Ubuntu 20.04 and Debian/Buster

Note: Creating the virtual environment using virtualenv -p python3 . does install a python interpreter however this problem still persists, and elpy-config is still the same.