标签归档:sublimetext2

如何像使用Sublime Text一样围绕PyCharm中的选定文本

问题:如何像使用Sublime Text一样围绕PyCharm中的选定文本

有没有一种方法可以配置PyCharm,使其能够仅通过键入括号键就可以将所选代码括在括号中,就像我们使用SublimText 2一样?

Is there a way to configure PyCharm to be able to surround selected code with parenthesis by just typing on the parenthesis key, like when we use SublimText 2?


回答 0

我想你想要类似的东西

Settings | Editor | General | Smart Keys -> Surround selection on typing quote or brace

I think you want something like

Settings | Editor | General | Smart Keys -> Surround selection on typing quote or brace


回答 1

PyCharm 4.0可以选择Surround With...,选择代码段并按

ctrl+ alt+T

或在Mac上:+ +T

选项1应该为您提供所需的功能:

PyCharm 4.0 has the option to Surround With..., by selecting your code snippet and pressing

ctrl + alt + T

or on Mac: + + T

Option 1 should provide you with the functionality you are looking for:


回答 2

Windows:打开pycharm并选择文件,设置,编辑器,智能键,在列表中,您将选中“键入引号或花括号时的环绕选择”,然后应用。 在此处输入图片说明

智能钥匙的pycharm位置的图像

Windows: open pycharm and select file, settings, Editor, Smart Keys, in the list you will check “Surround selection on typing quote or brace”, then apply. enter image description here

Image of pycharm location of smart keys


如何从Sublime Text 2运行Python代码?

问题:如何从Sublime Text 2运行Python代码?

我想在Sublime Text 2中设置完整的Python IDE。

我想知道如何从编辑器中运行Python代码。使用构建系统完成吗?我该怎么做 ?

I want to set up a complete Python IDE in Sublime Text 2.

I want to know how to run the Python code from within the editor. Is it done using build system? How do I do it ?


回答 0

工具->构建系统->(选择)Python然后:

跑步:

      Tools -> Build

      -or-

      Ctrl + B

      CMD + B  (OSX)

这将在控制台中启动您的文件,该控制台应位于编辑器的底部。

停止:

       Ctrl + Break or Tools -> Cancel Build

       Fn + C (OSX)

您可以在Break此处找到密钥的位置:http : //en.wikipedia.org/wiki/Break_key

注意:CTRL + C工作。

Ctrl + Break不起作用时该怎么办:

去:

首选项->键绑定-用户

并粘贴以下行:

{"keys": ["ctrl+shift+c"], "command": "exec", "args": {"kill": true} } 

现在,您可以使用ctrl+shift+c代替CTRL+BREAK

Tools -> Build System -> (choose) Python then:

To Run:

      Tools -> Build

      -or-

      Ctrl + B

      CMD + B  (OSX)

This would start your file in the console which should be at the bottom of the editor.

To Stop:

       Ctrl + Break or Tools -> Cancel Build

       Fn + C (OSX)

You can find out where your Break key is here: http://en.wikipedia.org/wiki/Break_key.

Note: CTRL + C will NOT work.

What to do when Ctrl + Break does not work:

Go to:

Preferences -> Key Bindings – User

and paste the line below:

{"keys": ["ctrl+shift+c"], "command": "exec", "args": {"kill": true} } 

Now, you can use ctrl+shift+c instead of CTRL+BREAK


回答 1

在Mac OS X上,以.py扩展名保存文件。按+ B。它在下面的窗口中运行。

On Mac OS X, save your file with a .py extension. Press + B. It runs in a window below.


回答 2

编辑%APPDATA%\ Sublime Text 2 \ Python \ Python.sublime-build

将内容更改为:

{
    "cmd": ["C:\\python27\\python.exe", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

将“ c:\ python27”部分更改为系统中具有的任何版本的python。

Edit %APPDATA%\Sublime Text 2\Python\Python.sublime-build

Change content to:

{
    "cmd": ["C:\\python27\\python.exe", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

change the “c:\python27” part to any version of python you have in your system.


回答 3

要运行CtrlB(通过回答matiit

但是当CtrlB 不起作用时,Sublime Text可能找不到Python解释器。尝试运行程序时,请参阅日志并在路径中找到对Python的引用。

[cmd:  [u'python', u'-u', u'C:\\scripts\\test.py']]
[path: ...;C:\Python27 32bit;...]

关键是它试图通过命令行运行python,cmd如下所示:

python -u C:\scripts\test.py

如果您无法从cmd运行python,那么Sublime Text也不会。
(在cmd中自己尝试,在其中键入python并运行它,应该显示python命令行)

您可以更改Sublime Text构建公式或System %PATH%

  • 要设置您的%PATH%
    * 您将需要重新启动编辑器以加载新的%PATH%

    • 运行命令行*,然后输入以下命令:*需要以管理员
      SETX /M PATH "%PATH%;<python_folder>"
      身份运行,例如:SETX /M PATH "%PATH%;C:\Python27;C:\Python27\Scripts"

    • 手动:(最好)在字符串末尾
      添加;C:\Python27;C:\Python27\Scripts

  • 要设置解释器的路径而不弄乱系统,%PATH%请参阅ppy的答案。

To RUN press CtrlB (answer by matiit)

But when CtrlB does not work, Sublime Text probably can’t find the Python Interpreter. When trying to run your program, see the log and find the reference to Python in path.

[cmd:  [u'python', u'-u', u'C:\\scripts\\test.py']]
[path: ...;C:\Python27 32bit;...]

The point is that it tries to run python via command line, the cmd looks like:

python -u C:\scripts\test.py

If you can’t run python from cmd, Sublime Text can’t too.
(Try it yourself in cmd, type python in it and run it, python commandline should appear)

SOLUTION

You can either change the Sublime Text build formula or the System %PATH%.

  • To set your %PATH%:
    *You will need to restart your editor to load new %PATH%

    • Run Command Line* and enter this command: *needs to be run as administrator
      SETX /M PATH "%PATH%;<python_folder>"
      for example: SETX /M PATH "%PATH%;C:\Python27;C:\Python27\Scripts"

    • OR manually: (preferable)
      Add ;C:\Python27;C:\Python27\Scripts at the end of the string.

  • To set the interpreter’s path without messing with System %PATH% see this answer by ppy.


回答 4

您可以使用SublimeREPL(首先需要安装Package Control)。

You can use SublimeREPL (you need to have Package Control installed first).


回答 5

如果使用python 3.x,则需要编辑 Python3.sublime-build

(首选项>浏览包> Python 3)

看起来像这样:

{
  "path": "/usr/local/bin",
  "cmd": ["python3", "-u", "$file"],
  "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
  "selector": "source.python"
}

If using python 3.x you need to edit the Python3.sublime-build

(Preferences > Browse packages > Python 3)

to look like this:

{
  "path": "/usr/local/bin",
  "cmd": ["python3", "-u", "$file"],
  "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
  "selector": "source.python"
}

回答 6

[这适用于ST3(Win),不确定ST2]

要使输出在Sublime中作为另一个文件可见(为错误显示一个文件),请执行以下操作:

  1. 创建一个新的构建系统: Tools > Build Systems > New Build System...
  2. 使用以下配置:
    {
        “ cmd”:[“ python.exe”,“ $ file”,“ 1>”,“ $ file_name .__ STDOUT __。txt”,“ 2>”,“ $ file_name .__ STDERR __。txt”],
        “ selector”:“ source.python”,
        “外壳”:是的,
        “ working_dir”:“ $ file_dir”
    }
  1. 对于您的Python文件,选择上面的构建系统配置文件: Tools > Build Systems > {your_new_build_system_filename}
  2. ctrl + b
  3. 现在,在文件旁边,例如,"file.py"您将拥有"file.__STDOUT__.py""file.__STDERR__.py"(对于错误,如果有的话)
  4. 如果将窗口分为三列或一个网格,您将立即看到结果,而无需切换面板/窗口

[ This applies to ST3 (Win), not sure about ST2 ]

To have the output visible in Sublime as another file (+ one for errors), do this:

  1. Create a new build system: Tools > Build Systems > New Build System...
  2. Use the following configuration:

    {
        "cmd": ["python.exe", "$file", "1>", "$file_name.__STDOUT__.txt", "2>", "$file_name.__STDERR__.txt"],
        "selector": "source.python",
        "shell": true,
        "working_dir": "$file_dir"
    }
  1. For your Python file select the above build system configuration file: Tools > Build Systems > {your_new_build_system_filename}
  2. ctrl + b
  3. Now, next to your file, e.g. "file.py" you’ll have "file.__STDOUT__.py" and "file.__STDERR__.py" (for errors, if any)
  4. If you split your window into 3 columns, or a grid, you’ll see the result immediately, without a need to switch panels / windows

回答 7

酷你们,我刚发现这个:

http://ptomato.wordpress.com/2012/02/09/geek-tip-running-python-guis-in-sublime-text-2/

它解释了(像上面的答案之一一样)如何在默认目录中编辑此exec.py。

我的问题是我的PYTHON UI APPLICATION无法启动。我从以下片段中注释掉了最后一行:

    # Hide the console window on Windows
    startupinfo = None
    if os.name == "nt":
        startupinfo = subprocess.STARTUPINFO()
        #startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

而且,taaadaaaa,我可以通过按Ctrl + B来启动我的应用。有趣的话,嗯?非常感谢您写这篇文章的人;-)

Cool U guys, I just found this:

http://ptomato.wordpress.com/2012/02/09/geek-tip-running-python-guis-in-sublime-text-2/

It explains (like one of the answers above) how to edit this exec.py in the default directory.

I had the problem that my PYTHON UI APPLICATION would not start. I commented out the last line from the following snipped:

    # Hide the console window on Windows
    startupinfo = None
    if os.name == "nt":
        startupinfo = subprocess.STARTUPINFO()
        #startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW

and, taaadaaaa, I could start my app by pressing Ctrl+B. Funny line anyways, uh? And a big thank you to whoever wrote that article ;-)


回答 8

您可以通过“查看/显示控制台”或Ctrl+ 访问Python控制台`

You can access the Python console via “View/Show console” or Ctrl+`.


回答 9

我解决了这个问题:

> Preferences –> Browse Packages –> Default 

打开第exec.py41-42行附近的文件,代码应如下所示:

for k, v in proc_env.iteritems():
    proc_env[k] = os.path.expandvars(v).encode(sys.getfilesystemencoding())

然后将其删除或编辑为:

try:    
    for k, v in proc_env.iteritems():
        proc_env[k] = os.path.expandvars(v).encode(sys.getfilesystemencoding())
except:
    print 'foobar'

I solved this problem :

> Preferences –> Browse Packages –> Default 

Open the exec.py file, near line 41-42, the code should look like this :

for k, v in proc_env.iteritems():
    proc_env[k] = os.path.expandvars(v).encode(sys.getfilesystemencoding())

then delete it or edit it as :

try:    
    for k, v in proc_env.iteritems():
        proc_env[k] = os.path.expandvars(v).encode(sys.getfilesystemencoding())
except:
    print 'foobar'

回答 10

我今天遇到了同样的问题。这是我设法在Sublime Text 3中运行python代码的方式:

  1. Ctrl+ B(对于Mac,+ B)开始构建系统。它应该立即执行文件。
  2. 遵循此答案以了解如何自定义构建系统。

接下来,您需要将内容替换Python.sublime-build

{
    "cmd": ["/usr/local/bin/python", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",
}

您当然可以进一步自定义它以适合您的需求。

I ran into the same problem today. And here is how I managed to run python code in Sublime Text 3:

  1. Press Ctrl + B (for Mac, + B) to start build system. It should execute the file now.
  2. Follow this answer to understand how to customise build system.

What you need to do next is replace the content in Python.sublime-build to

{
    "cmd": ["/usr/local/bin/python", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",
}

You can of course further customise it to something that works for you.


回答 11

在python v3.x中,您应该转到:Tools->Build System->New Build System

然后,在sublime文本编辑器中弹出untitled.sublime-build窗口,输入设置为:

{

    "cmd": ["path_to_the_python.exe","-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

要查看路径,请执行以下操作Type following in terminal as

python
>>> import sys
>>>print(sys.executable)

您可以制作多个构建系统,但默认情况下,应将其保存在具有.sublime-build扩展名的Sublime文本包中

然后,选择新的构建系统 ,然后cltr+b根据您的操作系统按或其他。

In python v3.x you should go to : Tools->Build System->New Build System.

Then, it pop up the untitled.sublime-build window in sublime text editor.Enter setting as:

{

    "cmd": ["path_to_the_python.exe","-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

To see the path, Type following in terminal as:

python
>>> import sys
>>>print(sys.executable)

You can make more than one Build System but it should default save inside Packages of Sublime text with .sublime-build extension.

Then, select the new Build System and press cltr+b or other based on your os.


回答 12

我有同样的问题。您可能尚未保存文件。确保使用.py扩展名保存您的代码,并且该代码应该可以工作。

I had the same problem. You probably haven’t saved the file yet. Make sure to save your code with .py extension and it should work.


回答 13

关于上述构建系统要注意的一件事:您可以编写(并使用)自定义.sublime-build文件,甚至可以编写每个项目build_systems子句(在项目设置中)。这使您可以做一些有用的事情,例如使用ANSI颜色输出的精美测试运行程序

要获得更多的“完整IDE”功能,可以使用出色的SublimePythonIDE软件包:

  • 代码完成(英特尔)
  • 跳转到定义和对象描述
  • 适当的棉绒/ pep8
  • 用virtualenv支持不同的解释器

披露:我已经贡献了PR该程序包,我用它所有的时间,但 其他人

One thing to note about the aforementioned build system: you can write (and use) custom .sublime-build files or even per project build_systems clause (in your project settings). This allows you to do useful things like a fancy test runner with ANSI colors output.

For even more “full IDE” features, you can use the excellent SublimePythonIDE package:

  • code completion (intel)
  • jump to definition & object description
  • proper linting/pep8
  • supports different interpreters with virtualenv

Disclosure: I’ve contributed a PR to that package, and I use it all the time, but there are others.


回答 14

在Sublime旁边使用真正的python控制台

Sublime的构建系统和SublimeREPL(上面的答案)都受到限制,因为您在运行文件后无法轻松地与工作空间变量进行交互。

如果要运行脚本,然后以类似于REPL的方式工作(就像在IDE中一样),则建议将Sublime与IPython控制台一起打开。您可以使用AutoHotKey(Windows)或AutoKey(Linux)进行设置,以使单个快捷方式将复制文件名(或仅复制所选的代码),然后将其粘贴到控制台中以运行文件。

LinuxWindows的详细说明

Use a real python console alongside Sublime

Both Sublime’s build system and SublimeREPL (the answers above) are limited in that you can’t easily interact with the workspace variables after you run your file.

If you want to run a script, then work in a REPL-like fashion (like you would in an IDE), then I recommend having Sublime open alongside an IPython console. Using AutoHotKey (Windows) or AutoKey (Linux), you can set this up such that a single shortcut will copy the filename (or just the selected code) and then paste this in the console to run the file.

Detailed instructions for Linux or Windows


PEP8的E128:连续行缩进不足以实现视觉缩进是什么?

问题:PEP8的E128:连续行缩进不足以实现视觉缩进是什么?

刚刚用Sublime Text(使用Sublime Linter)打开了一个文件,并注意到了一个我以前从未见过的PEP8格式错误。这是文本:

urlpatterns = patterns('',
    url(r'^$', listing, name='investment-listing'),
)

它标记了第二个参数,即开始的行 url(...)

我本来打算在ST2中禁用此检查,但是在忽略它之前,我想知道自己在做错什么。你永远不会知道,如果它看起来很重要,我什至可以改变我的方式:)

Just opened a file with Sublime Text (with Sublime Linter) and noticed a PEP8 formatting error that I’d never seen before. Here’s the text:

urlpatterns = patterns('',
    url(r'^$', listing, name='investment-listing'),
)

It’s flagging the second argument, the line that starts url(...)

I was about to disable this check in ST2 but I’d like to know what I’m doing wrong before I ignore it. You never know, if it seems important I might even change my ways :)


回答 0

如果在第一行上放置任何内容,PEP-8建议您在括号内缩进一行,因此应该在括号内缩进:

urlpatterns = patterns('',
                       url(r'^$', listing, name='investment-listing'))

或不将任何参数放在起始行上,然后缩进一个统一级别:

urlpatterns = patterns(
    '',
    url(r'^$', listing, name='investment-listing'),
)

urlpatterns = patterns(
    '', url(r'^$', listing, name='investment-listing'))

我建议您通读PEP-8-您可以浏览其中的很多内容,而且与某些技术性更高的PEP相比,它很容易理解。

PEP-8 recommends you indent lines to the opening parentheses if you put anything on the first line, so it should either be indenting to the opening bracket:

urlpatterns = patterns('',
                       url(r'^$', listing, name='investment-listing'))

or not putting any arguments on the starting line, then indenting to a uniform level:

urlpatterns = patterns(
    '',
    url(r'^$', listing, name='investment-listing'),
)

urlpatterns = patterns(
    '', url(r'^$', listing, name='investment-listing'))

I suggest taking a read through PEP-8 – you can skim through a lot of it, and it’s pretty easy to understand, unlike some of the more technical PEPs.


回答 1

对于这样的语句(由PyCharm自动格式化)也是如此:

    return combine_sample_generators(sample_generators['train']), \
           combine_sample_generators(sample_generators['dev']), \
           combine_sample_generators(sample_generators['test'])

它将发出相同的样式警告。为了摆脱它,我不得不将其重写为:

    return \
        combine_sample_generators(sample_generators['train']), \
        combine_sample_generators(sample_generators['dev']), \
        combine_sample_generators(sample_generators['test'])

This goes also for statements like this (auto-formatted by PyCharm):

    return combine_sample_generators(sample_generators['train']), \
           combine_sample_generators(sample_generators['dev']), \
           combine_sample_generators(sample_generators['test'])

Which will give the same style-warning. In order to get rid of it I had to rewrite it to:

    return \
        combine_sample_generators(sample_generators['train']), \
        combine_sample_generators(sample_generators['dev']), \
        combine_sample_generators(sample_generators['test'])