Dennis
Dennis

Reputation: 165

emacs elpy custom python module not found

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.

Upvotes: 1

Views: 634

Answers (1)

Gwang-Jin Kim
Gwang-Jin Kim

Reputation: 9865

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.

Upvotes: 1

Related Questions