質問

I've been using ST2 for quite some time now and given that python is now my primary language, I decided to try PyCharm. It's somewhat cluttered and my love for Sublime is keeping me away. However, PyCharm is somehow able to do the following things which Sublime can't:

  1. Typing a period after an object and getting the CORRECT list of all its attributes (Sublime can do this, sort of, except it returns random attributes)
  2. Refactoring of variables across entire projects/folders
  3. Show docstrings for methods defined with them

It has more important functions which Sublime can't do quite yet. I have many packages already installed in Sublime which can almost do what PyCharm can except for those 3 things above.

So back to the question: How can PyCharm supply this functionality given that Python is not a strongly typed language?

役に立ちましたか?

解決

If you're looking for better autocompletion, try SublimeCodeIntel, available via Package Control. It takes a little while to index your built-in modules and third-party packages, but once it's ready it's quite helpful. For example, with Pandas I can define a dataframe:

df = pd.DataFrame(some_input_data)

then type df. and all the associated classes and methods of a DataFrame object come up. To set this up, you'll need to add the following to your user preferences (Preferences -> Settings - User):

"auto_complete_triggers":
[
    {
        "characters": ".",
        "selector": "source"
    }
]

along with any other triggers you may have.

UPDATE

While SublimeCodeIntel is a decent package, it doesn't always work well - it sometimes has issues finding newly-installed modules, the database can get corrupted, there can sometimes be significant delays in autocompletion, it has trouble with virtualenvs, and sometimes it just doesn't work. If you're using Sublime Text 3, I highly recommend using Anaconda instead (no relation to the Anaconda Python distribution). Once you set it up (a very brief process, basically you just specify which Python interpreter you'd like to use), it just works. There's no database to initialize or get corrupted, it automatically discovers when you've added new packages, it's very unobtrusive running in the background... I can't say enough good things about it. It uses the JEDI autocompletion module, among other things, and is fast and accurate. It automatically determines what type variables are, and fills in the completions with the appropriate methods and classes that can be called on it. You can have it do parameter completion as well, but that got a little annoying for me, so I turned it off. One thing it can't do is method chaining, but nothing's perfect. It also includes modules for code complexity checking and linting, which is fine, but I don't need it, and only want to lint when I want to lint, so I turned that off as well. The other major difference between it and SublimeCodeIntel is that Anaconda is Python-specific, while SCI supports a number of different languages.

I'd highly recommend giving Anaconda a try. Aside from the method chaining, I've been very happy with it, and haven't gone back. One cool thing you can do is assign different values to the "python_interpreter" setting in your project files, so you can easily use virtualenvs, or (like I do) have one project open for Python 2 coding, and another for Python 3.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top