Coder Social home page Coder Social logo

importmagic.el's Introduction

importmagic.el Build Status MELPA

importmagic.el is an Emacs package inspired on @alecthomas's importmagic library and PyCharm's ability to suggest imports for unresolved symbols.

Example of Import Magic at work

WARNING!

PLEASE use wrappers for virtual environments as importmagic.el does not care at all about the environment on which it is run. importmagic.el is known to work with virtualenvwrapper.el and pyvenv.

If you still don't want to do that, check out the Usage section.

Installation

To install this package, MELPA is the way to go, but you also need some extra dependencies.

Python dependencies

This package relies heavily on importmagic and EPC. You can get them from pip. These packages should be installed in the same environment you're working on.

$ pip install importmagic epc

Installing the Emacs package

It is recommended that you install this package from MELPA. There's still a way if you don't use it though.

From MELPA

A simple way would be to just:

M-x package-install RET importmagic RET

You can also try use-package:

(use-package importmagic
    :ensure t
    :config
    (add-hook 'python-mode-hook 'importmagic-mode))

The above example is the minimal configuration in order to get started with importmagic.

Whichever way you choose remember to add the hook to python mode:

(add-hook 'python-mode-hook 'importmagic-mode)

Without MELPA

Download both importmagic.el and importmagicserver.py. Place them on a load-path of your emacs directory. For instance: ~/.emacs.d/site-lisp

If you haven't already, tell emacs you want to load files from that directory:

(add-to-list 'load-path (expand-file-name (concat user-emacs-directory "site-lisp/")))

Of course, you can choose to change the name of the site-lisp portion of the code. Don't forget to add the mode to Python buffers, put this line anywhere in your .emacs or init.el

(add-hook 'python-mode-hook 'importmagic-mode)

Usage

The default behavior sets only one key binding: C-c C-l. It solves imports for every unresolved symbol in the buffer, prompting for one import at a time. If there are no imports found for a given symbol, importmagic will let you know at the end of the process.

By default, importmagic.el will recursively index every symbol from the current buffer's directory, which means you should get fairly accurate suggestions for imports you might need.

Key bindings

Every key binding is under the importmagic-mode-map. If you don't like the C-c C-l keybinding or want to add extra keys to your configuration, just set them like so:

(define-key importmagic-mode-map (kbd "C-c C-f") 'importmagic-fix-symbol-at-point)

Note that the example above will override a defined key binding in the python-mode-map. You can do that as long as you feel the need to (as I did). This package is not really intended to interfere with the default bindings, though.

Imports style

Importmagic supports configuration on the styles it has. As of July 2, 2017, so does importmagic.el.

When an import statement gets too long, you have the choice to group it using either parentheses or backslashes. Both of these options (maximum length of line and import group styles) can be set with the variable importmagic-configuration-style-alist, which is an alist (duh) of your preferences.

The default value is

'((multiline . parentheses)
  (max_columns . 79))

Customizing

M-x customize-group RET importmagic RET.

Python Interpreter

As we talked about in the first section of this document, importmagic.el won't care about the environment and will use the default system python interpreter. If you do not want to do this and want to use a different python interpreter you can set importmagic-python-interpreter like so:

(setq importmagic-python-interpreter "/path/to/my/virtualenv/bin/python")

Annoyances

Every package has its own annoyances, and this one is no exception. I'll try to describe here how to get rid of annoyances this package may produce.

Non-error messages

importmagic.el can be very verbose when you develop Python for several hours. In fact, it was suggested that I gave the possibility to supress these messages. You can do so by setting the variable importmagic-be-quiet to t like so:

(setq importmagic-be-quiet t)

This, however, will not supress error messages.

Mode line

If importmagic gets your mode line too cluttered, try diminish. Something like this would be fine:

(diminish 'importmagic-mode)

Buffers

importmagic.el uses an EPC server to query for symbols. While it's very convenient for the developer, it's not so good for the user, because it generates one buffer with the EPC connection for every Python buffer you open. While that's ok (at least for me), It can be troublesome for some people.

Helm users can get rid of these buffers (as in not see them) by evaluating the following expression:

(add-to-list 'helm-boring-buffer-regexp-list "\\*epc con")

Likewise, ivy users can get rid of it with the following:

(add-to-list 'ivy-ignore-buffers "\\*epc con")

For ido users, no idea, Sorry!

Provided functions

A list of every provided function, in case you either want to bind them to a key or just want to M-x for it.

importmagic-mode

Turn on/off importmagic-mode in the current buffer. As usual, a positive argument sets it on and a negative argument sets it off. With just M-x, it will toggle the mode.

importmagic-fix-imports

Query for every unimported symbol in the current buffer. This can be useful if you've written a lot of code and didn't bother to import anything.

importmagic-fix-symbol-at-point

Query for imports for the symbol at point. Note that this will query the database even if the symbol at point is already imported. Flycheck can be helpful for this one.

importmagic-fix-symbol

Prompts for a symbol to import. This function is the base for the two above since it doesn't make any assumption on what you want to import. It will find suitable candidates for the given symbol. Note that it can import symbols that you're not currently using.

importmagic-update-index

Updates index for the current file. This can be useful if something changed in the current directory outside the current buffer and you need to import symbols from those modified files.

Virtual environments

importmagic.el is known to work with pyvenv and virtualenvwrapper.el. Other packages have not been tested.

Note that the above will mean that if either importmagic or epc are not in your virtual env, it will fail. Don't worry too much though. If importmagic fails, it will give you a warning, but it will not get in your way.

Known issues

There seems to be an issue going on with Gtk symbols. It doesn't only affect importmagic, but it also affects Jedi. See this issue.

Contributing

Any kind of contribution is absolutely welcome.

If you haven't already, install Cask. Run the tests with:

$ cask exec ert-runner

Note that you'll need ert-runner for that.

Send me pull-requests!

importmagic.el's People

Contributors

anachronic avatar danlamanna avatar jgarte avatar syohex avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

importmagic.el's Issues

Expose importmagic style configuration

I might be missing something, but it seems that the style configuration for importmagic isn't exposed.

From a brief look, maybe it makes sense for the get_import_statement to take a configuration alist and convert it to a dictionary (not sure how epc handles this) to set on the Imports object. Does that make sense/sound about right?

Importmagic and/or epc not found. importmagic.el will not be working.

I am using the spacemacs python layer on macos and I am running into this error.

I have installed importmagic and epc using pip globally.

➜ pip install importmagic epc Requirement already satisfied: importmagic in /usr/local/lib/python3.7/site-packages (0.1.7) Requirement already satisfied: epc in /usr/local/lib/python3.7/site-packages (0.0.5) Requirement already satisfied: setuptools>=0.6b1 in /usr/local/lib/python3.7/site-packages (from importmagic) (41.6.0) Requirement already satisfied: sexpdata>=0.0.3 in /usr/local/lib/python3.7/site-packages (from epc) (0.0.3)

I've installed importmagic and epc for python2 as well.

I have set my python interpreter in emacs to "python3"

(setq flycheck-python-pycompile-executable "python3") (setq python-shell-interpreter "python3")

All other python features work except this one. Not sure how to resolve this.

how to use this package with emacs's elpy package?

hello, i use elpy mode to write python code im emacs, but the latest elpy mode remove the importmagic package, some command is no longer to use, such as elpy-importmagic-fixup, it's very conveniont, is there any method to implete it in importmagic.el?

Archive this repository or find a new maintainer

I have not been using Emacs now for a very long time, and I don't have time to maintain this project. In fact, #17 has stayed open for a very long time, and I'll admit I haven't even taken a look at it.

I'm going to archive this repository unless I can find a new maintainer before Jan 31, 2020. If anyone reading this wishes to maintain this, project, please comment here.

Improve test suite

We're currently relying on travis to set up virtual environments. However, I'm not 100% convinced that emacs is using the virtual environments provided by the OS, since nowhere are we sourcing those virtualenvs. This leads me to think that emacs is always using OS's python rather than the virtual environment's.

virtualenvwrapper.el seems like a good place to look at.

I'd like to make a macro that defines tests for every Python version we want to support. That way we can keep our definitions and be sure that we're sourcing the right python versions.

Cannot find importmagic and epc

I'm using pip3 to install these dependencies. I tried pip as well but no luck get this working.

After installing the dependencies. I get:

Requirement already satisfied: importmagic in /usr/local/lib/python3.6/site-packages
Requirement already satisfied: epc in /usr/local/lib/python3.6/site-packages
Requirement already satisfied: setuptools>=0.6b1 in /usr/local/lib/python3.6/site-pack
ages (from importmagic)                                                              
Requirement already satisfied: sexpdata>=0.0.3 in /usr/local/lib/python3.6/site-packag
es (from epc)                                                                        

And I get the error message when opening a python file.

Importmagic and/or epc not found. importmagic.el will not be working.

Do I have to configure this just like what this sublime plugin did here?

doesn't work with tree-sitter

There's a new mode in emacs29, python-base-mode, which python-mode and python-ts-mode both derive from, and importmagic ought to test against that.

Python 3.9 seg fault

I faced seg fault on my emacs importmagic. During deep investigation I found out that problem appears on newer versions of python. I could not find reason for that. But I found workaround that wont harm. And during lack of support for original python importmagic. I would be happy to see fixes at least here.

What needs to be done? Add name='default' here

Link to importmagic issue

Use python-shell-interpreter instead of hardcoded python.

This has been requested in #3 and would solve #10 if error messages were more accurate. Issue can be closed after

  1. Error messages for importmagic and/or epc are not found and running python process failed (which do not exist right now) are separated. Right now the default behavior is having only the first error message for both cases.
  2. importmagic.el uses the python-shell-interpreter variable in order to find the python executable. This was ultimately the problem in #10.
  3. Such cases have tests.

I'll mention #12 aims to fix this but it's a little buggy atm.

Process epc con 3 not running

When running importmagic-fix-symbol, I keep getting the error message:

epc:net-send: Process epc con 3 not running

I cannot find any info online why this happens. I do have a buffer called *epc con 6*. Why does importmagic need "epc con 3"? What is special about it?

How can I make importmagic.el to find modules installed under `pip install -e .`?

I have done pip install -e . in my project where I have to import all the files as:

from broker._utils._log import log
from broker._utils.tools import QuietExit, print_tb
from broker.utils import tools
$ tree
├── broker
│   ├── _utils
│       ├── _log.py
│       ├── tools.py

But importmagic does not find any modue names under broker like import broker._utils._log.py.

Would it be possible to force it to see them as well?

broken by sexpdata 0.0.4

This issue affects importmagic.el in a slightly different way than jedi.el and I don't know where to look for a log that contains a traceback, but it hangs forever on 0.0.4 and works fine on 0.0.3: jd-boyd/sexpdata#36

don't add current directory

When I auto-complete in a directory like this:

+ src/
    + foo/
        | bar.py
        | baz.py
        | __init__.py

if I'm in baz.py and I type BazClass, a symbol that appears in bar.py, I will see suggestions like this:

from foo.baz import BazClass
from baz import BazClass

the latter seems to be straightforwardly incorrect. Is there some way I can configure importmagic to get rid of it?

from decimal import Decimal never suggested for Decimal

I assume this is because it's a C module and therefore there's no source to scan. I'm opening this on .el rather than the Python library because the Python library has a way to add import statements but it's not clear to me what the "right" way to inject those into the relevant process is.

Feature request: Import parent module instead of submodule

Hi, thanks for the awesome package.

Currently for os.path.join* (where * is the cursor), importmagic-fix-symbol-at-point will ask imports for os.path which give candidates like

Screen Shot 2019-04-02 at 22 36 48

It would be nice to have an option to ask imports for os even the cursor is right after os.path.join like

Screen Shot 2019-04-02 at 22 32 48

for one who prefers usages like os.path.join() and datetime.datetime.now() instead of join() and now().

Also, if the cursor is right after os.path*, importmagic-fix-symbol-at-point, candidates is now given as

Screen Shot 2019-04-02 at 22 31 25

which (importing path while using as os.path) is probably not what the user want in this situation.

How do you think about it? I found this because I saw the gif of the importmagic repo and noticed that it imports os in this situation.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.