1

I have my emacs configured with elpy as the following:

 ;;; for python 
 (elpy-enable)
 (setq elpy-modules (delq 'elpy-module-flymake elpy-modules))
 (add-hook 'elpy-mode-hook 'flycheck-mode)
 (pyenv-mode)
 (setq elpy-shell-use-project-root nil)
 (setq python-shell-interpreter "python3")

however, when writing a simple python script, if I tried to add a custom python module in the same directory. evaluate in elpy-shell by typing ctrl-c ctrl-c complains that the module is not there.

    import restaurant
    ImportError: No module named restaurant

if I evaluate the python script directly in the terminal, it works fine. It somehow did not config the elpy shell right, it seems not finding the python module in current directory.

any help is appreciated.

Dennis
  • 159
  • 1
  • 11
  • obviously the python you use from within emacs is not the same like the python you use in console. Do you use virtual environemnts (conda or virtuaenv)? – Gwang-Jin Kim Jun 24 '20 at 10:14
  • I do use virtualenv, but the missing module error is there regardless if I activate the virtual environment. I used python3 in the terminal as well, why they are different? – Dennis Jun 24 '20 at 15:37
  • ah you didn't install the module - but in same directory in terminal - well, then you should open emacs also in same directory – Gwang-Jin Kim Jun 25 '20 at 08:56
  • or give path to same directory import folder1.folder2. ... .restaurant – Gwang-Jin Kim Jun 25 '20 at 08:57

1 Answers1

1

Make out of your restuarant.py a custom package.

quickest way:

# in terminal
mkdir -p restaurant/restaurant
cd restaurant
touch setup.py

copypaste into setup.py:

from setuptools import setup

setup(
    name="restaurant",
    version="0.0.1",
    description="<describe what it does here>",
    packages=["restaurant"],
    author="yourname",
    author_email="youremail",
    keywords=["restaurant"],
    url="<url in github or bitbucket? - not necessary>"
)

save it. then

# in terminal
cd restaurant # the inner folder
# put into the inner restaurant folder your restaurant.py
touch __init__.py # python package needs it to recognize restaurant.py
# put there in:
from .restaurant import *
# or import each function/variable/dict etc what should be visible outside
# of package

So, now you can locally install using pip from your console python3:

# in terminal
pip install -e /path/to/your/outer/folder/restaurant

restart python3 from now on, it should be available from everywhere

import restaurant

whenever you put changes into restaurant.py, it should actually be visible immediately. But if not, reinstall with same pip command!

Creating custom package is so easy. I should have began with it long ago to centralize my code and make it available from everywhere.

Gwang-Jin Kim
  • 9,303
  • 17
  • 30