syl20bnr / spacemacs

A community-driven Emacs distribution - The best editor is neither Emacs nor Vim, it's Emacs *and* Vim!
http://spacemacs.org
GNU General Public License v3.0
23.64k stars 4.89k forks source link

run python src block in org got error #12251

Closed csfreebird closed 4 years ago

csfreebird commented 5 years ago

I am using virtualenv python3 in spacemacs.

   #+BEGIN_SRC python
     from sklearn.feature_extraction.text import CountVectorizer  
     vectorizer = CountVectorizer()
     corpus = ["I come to China to travel", "This is a car polupar in China", "I love tea and Apple ", "The work is to write some papers in science"]
     print(vectorizer.fit_transform(corpus))
   #+END_SRC

press C-c C-c to execute the above block, got error message:

Traceback (most recent call last):
  File "<stdin>", line 8, in <module>
  File "<stdin>", line 3, in main
ImportError: No module named sklearn.feature_extraction.text

If I run the above code in python3 shell, it works.

Compro-Prasad commented 5 years ago

I you are unable to activate virtualenv then see https://emacs.stackexchange.com/questions/24267/using-org-babel-to-enable-virtualenv

I have used something like:

* Some heading
:PROPERTIES:
:header-args:python: :session test-venv :results output
:header-args:sh: :session test-venv
:END:
#+begin_src elisp
(pyvenv-workon "test-venv-z-XQp3ap")
#+end_src

#+RESULTS:

#+begin_src sh
echo $VIRTUAL_ENV
which python
#+end_src

#+RESULTS:
| /home/compro/.virtualenvs/test-venv-z-XQp3ap            |
| /home/compro/.virtualenvs/test-venv-z-XQp3ap/bin/python |

#+begin_src python
import requests
#+end_src

#+RESULTS:
: Traceback (most recent call last):
:   File "<stdin>", line 1, in <module>
: ModuleNotFoundError: No module named 'requests'

#+begin_src sh :results silent
pipenv install requests
#+end_src

#+begin_src python
  import requests
  print("Hello")
#+end_src

#+RESULTS:
: Hello
github-actions[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Please let us know if this issue is still valid!