內容包含:pycharm學習技巧 Learning tips、PyCharm3.0默認快捷鍵(翻譯的)、pycharm經常使用設置、pycharm環境和路徑配置、Pycharm實用拓展功能:pycharm中清除已編譯.pyc中間文件、python2轉python3最快方式
[pycharm版本控制和數據庫管理][PyCharm中的那些實用功能]
html
/pythoncharm/help/tip of the day:
A special variant of the Code Completion feature invoked by pressing Ctrl+Space twice allows you to complete the name of any class no matter if it was imported in the current file or not. If the class is not imported yet, the import statement is generated automatically.
You can quickly find all places where a particular class, method or variable is used in the whole project by positioning the caret at the symbol's name or at its usage in code and pressing Alt+Shift+F7 (Find Usages in the popup menu).
To navigate to the declaration of a class, method or variable used somewhere in the code, position the caret at the usage and press F12. You can also click the mouse on usages with the Ctrl key pressed to jump to declarations.
You can easily rename your local variables with automatic correction of all places where they are used.
To try it, place the caret at the variable you want to rename, and press Shift+F6 (Refactor | Rename). Type the new name in the popup window that appears, or select one of the suggested names, and press Enter.python
...mysql
切換
Use Alt+Up and Alt+Down keys to quickly move between methods in the editor.
Use Ctrl+Shift+F7 (Edit | Find | Highlight Usages in File) to quickly highlight usages of some variable in the current file.
選擇
You can easily make column selection by dragging your mouse pointer while keeping the Alt key pressed.
補全
Working in the interactive consoles, you don't need to memorise the command line syntax or available functions. Instead, you can use the familiar code completion Ctrl+Space. Moreover, from within the lookup list, you can press Ctrl+Q to view the item's documentation.
顯示
Use F3 and Shift+F3 keys to navigate through highlighted usages.
Press Escape to remove highlighting.
歷史
Ctrl+Shift+Backspace (Navigate | Last Edit Location) brings you back to the last place where you made changes in the code.
Pressing Ctrl+Shift+Backspace a few times moves you deeper into your changes history.
Ctrl+E (View | Recent Files) brings a popup list of the recently visited files. Choose the desired file and press Enter to open it.
Use Alt+Shift+C to quickly review your recent changes to the project.
剪切板
Use the Ctrl+Shift+V shortcut to choose and insert recent clipboard contents into the text.
If nothing is selected in the editor, and you press Ctrl+C, then the whole line at caret is copied to the clipboard.
run/debug
By pressing Alt+Shift+F10 you can access the Run/Debug dropdown on the main toolbar, without the need to use your mouse.
git
在PyCharm安裝目錄 /opt/pycharm-3.4.1/help目錄下能夠找到ReferenceCard.pdf快捷鍵英文版說明 or 打開pycharm > help > default keymap refweb
PyCharm Default Keymapsql
一、編輯(Editing)shell
Ctrl + Space 基本的代碼完成(類、方法、屬性)Ctrl + Q 快速查看文檔數據庫
F1 外部文檔
django
Shift + F1 外部文檔,進入web文檔主頁ubuntu
Ctrl + Shift + Z --> Redo 重作
Alt + Right/Left 跳轉到下一個、前一個編輯的選項卡(代碼文件)
Alt + Up/Down跳轉到上一個、下一個方法
F12 回到先前的工具窗口Ctrl+Shift+Backspace 導航到最近編輯區域 {差很少就是返回上次編輯的位置}
Alt + F1 查找當前文件或標識Ctrl + U跳轉到父方法、父類
Ctrl + ]/[跳轉到代碼塊結束、開始
Ctrl + F12彈出文件結構Ctrl + Tab在窗口間進行切換
lz提示一下,pycharm中的設置是能夠導入和導出的,file>export settings能夠保存當前pycharm中的設置爲jar文件,重裝時能夠直接import settings>jar文件,就不用重複配置了。
file -> Setting ->Editor
1. 設置Python自動引入包,要先在 >general > autoimport -> python :show popup
快捷鍵:Alt + Enter: 自動添加包2. 「代碼自動完成」時間延時設置
> Code Completion -> Auto code completion in (ms):0 -> Autopopup in (ms):500
3. Pycharm中默認是不能用Ctrl+滾輪改變字體大小的,能夠在〉Mouse中設置
4. 顯示「行號」與「空白字符」
> Appearance -> 勾選「Show line numbers」、「Show whitespaces」、「Show method separators」
5. 設置編輯器「顏色與字體」主題
> Colors & Fonts -> Scheme name -> 選擇"monokai"「Darcula」
說明:先選擇「monokai」,再「Save As」爲"monokai-pipi",由於默認的主題是「只讀的」,一些字體大小顏色什麼的都不能修改,拷貝一份後方可修改!
修改字體大小
> Colors & Fonts -> Font -> Size -> 設置爲「14」
6. 設置縮進符爲製表符「Tab」
File -> Default Settings -> Code Style
-> General -> 勾選「Use tab character」
-> Python -> 勾選「Use tab character」
-> 其餘的語言代碼同理設置
7. 去掉默認摺疊 > Code Folding -> Collapse by default -> 所有去掉勾選
> Editor Tabs -> Mark modified tabs with asterisk 打上勾
9.>file and code template>python scripts
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__ = '$Package_name'
__author__ = '$USER'
__mtime__ = '$DATE'
# code is far away from bugs with the god animal protecting
I love animals. They taste delicious.
┏┓ ┏┓
┏┛┻━━━┛┻┓
┃ ☃ ┃
┃ ┳┛ ┗┳ ┃
┃ ┻ ┃
┗━┓ ┏━┛
┃ ┗━━━┓
┃ 神獸保佑 ┣┓
┃ 永無BUG! ┏┛
┗┓┓┏━┳┓┏┛
┃┫┫ ┃┫┫
┗┻┛ ┗┻┛
"""
10 python文件默認編碼
File Encodings> IDE Encoding: UTF-8;Project Encoding: UTF-8;
11. 代碼自動整理設置
這裏line breaks去掉√,不然bar, 和baz會分開在不一樣行,很差看。
File -> Settings -> appearance
1. 修改IDE快捷鍵方案
> Keymap
1) execute selection in console : add keymap > ctrl + enter
系統自帶了好幾種快捷鍵方案,下拉框中有如「defaul」,「Visual Studio」,在查找Bug時很是有用,「NetBeans 6.5」,「Default for GNOME」等等可選項,
由於「Eclipse」方案比較大衆,我的用的也比較多,最終選擇了「Eclipse」。
仍是有幾個經常使用的快捷鍵跟Eclipse不同,爲了能修改,還得先對Eclipse方案拷貝一份:
(1).代碼提示功能,默認是【Ctrl+空格】,現改成跟Eclipse同樣,即【Alt+/】
Main menu -> code -> Completion -> Basic -> 設置爲「Alt+/」
Main menu -> code -> Completion -> SmartType -> 設置爲「Alt+Shift+/」
不過「Alt+/」默認又被
Main menu -> code -> Completion -> Basic -> Cyclic Expand Word 佔用,先把它刪除再說吧(單擊右鍵刪除)!
(2).關閉當前文檔,默認是【Ctrl+F4】,現改成跟Eclipse同樣,即【Ctrl+W】
Main menu -> Window -> Active Tool Window -> Close Active Tab -> 設置爲 「Ctrl+F4」;
Main menu -> Window -> Editor -> Close -> 設置爲 「Ctrl+W」;2.設置IDE皮膚主題
> Theme -> 選擇「Alloy.IDEA Theme」
或者在setting中搜索theme能夠改變主題,全部配色統一改變
File > settings > build.excution
每次打開python控制檯時自動執行代碼
> console > pyconsole
import sys # print('Python %s on %s' % (sys.version, sys.platform)) sys.path.extend([WORKING_DIR_AND_PYTHON_PATHS]) import os print('current workdirectory : ', os.getcwd() ) import numpy as np import scipy as sp import matplotlib as mpl
若是安裝了ipython,則在pyconsole中使用更強大的ipython
> console
選中use ipython if available
這樣每次打開pyconsole就會打開ipython
Note: 在virtualenv中安裝ipython: (ubuntu_env) pika:/media/pika/files/mine/python_workspace/ubuntu_env$pip install ipython
File > settings > Languages & Frameworks
若是在項目設置中開啓了django支持,打開python console時會自動變成打開django console,固然若是不想這樣就關閉項目對django的支持:
若是打開支持就會在 settings > build.excution > console下多顯示一個django console:
Django console設置以下
import sys print('Python %s on %s' % (sys.version, sys.platform)) import django print('Django %s' % django.get_version()) sys.path.extend([WORKING_DIR_AND_PYTHON_PATHS]) if 'setup' in dir(django): django.setup() import django_manage_shell; django_manage_shell.run(PROJECT_ROOT)
File > settings > Project : initial project
project dependencies > LDA > project depends on these projects > 選擇sim_cluster就能夠在LDA中調用sim_cluster中的包
用於配置python項目執行的python路徑
好比,有的項目是運行的是系統python2.7下的環境;有的是3.4;有的項目使用的是virtualenv的python環境[python虛擬環境配置 - pycharm中的項目配置]
在pycharm > file > settings > project:pythonworkspace > project interpreter > 選擇對應項目 > project interpreter中指定python解釋器
pycharm中運行configuration有一個選項add content roots to pythonpath
選中後sys.path中會多一整個項目project的路徑/media/pika/files/mine/python_workspace,裏面的目錄就被當成包使用,這樣就能夠經過from SocialNetworks.SocialNetworks引入不是python包的目錄中的文件了。
不過最好使用sys.path.append(os.path.join(os.path.split(os.path.realpath(__file__))[0],"../.."))來添加,這樣在pycharm外也能夠運行不出錯 。
pycharm中的項目中能夠包含package、目錄(目錄名能夠有空格)、等等
目錄的某個包中的某個py文件要調用另外一個py文件中的函數,首先要將目錄設置爲source root,這樣才能從包中至上至上正確引入函數,不然怎麼引入都出錯:
SystemError: Parent module '' not loaded, cannot perform relative import
Note:目錄 > 右鍵 > make directory as > source root
ctrl + shift + f10 / f10 執行python腳本時
當前工做目錄cwd爲run/debug configurations 中的working directory
可在edit configurations > project or defaults中配置
python console中執行時
cwd爲File > settings > build.excution > console > pyconsole中的working directory
並可在其中配置
pycharm中os.environ不能讀取到terminal中的系統環境變量
pycharm中os.environ不能讀取.bashrc參數
使用pycharm,不管在python console仍是在module中使用os.environ返回的dict中都沒有~/.bashrc中的設置的變量,可是有/etc/profile中的變量配置。然而在terminal中使用python,os.environ卻能夠獲取~/.bashrc的內容。
解決方法1:
在~/.bashrc中設置的系統環境只能在terminal shell下運行Spark程序纔有效,由於.bashrc is only read for interactive shells.
若是要在當前用戶整個系統中都有效(包括pycharm等等IDE),就應該將系統環境變量設置在~/.profile文件中。若是是設置全部用戶整個系統,修改/etc/profile或者/etc/environment吧。
如SPARK_HOME的設置[Spark:相關錯誤總結 ]
解決方法2:在代碼中設置,這樣無論環境有沒有問題了
# spark environment settings import sys, os os.environ['SPARK_HOME'] = conf.get(SECTION, 'SPARK_HOME') sys.path.append(os.path.join(conf.get(SECTION, 'SPARK_HOME'), 'python')) os.environ["PYSPARK_PYTHON"] = conf.get(SECTION, 'PYSPARK_PYTHON') os.environ['SPARK_LOCAL_IP'] = conf.get(SECTION, 'SPARK_LOCAL_IP') os.environ['JAVA_HOME'] = conf.get(SECTION, 'JAVA_HOME') os.environ['PYTHONPATH'] = '$SPARK_HOME/python/lib/py4j-0.10.3-src.zip:$PYTHONPATH'
參考[Spark安裝和配置]
選中你的workspace > 右鍵 > clean python compiled files
還能夠本身寫一個清除代碼
[python小工具 ]針對當前pycharm中打開的py文件對應的目錄刪除其中全部的pyc文件。若是是直接運行(而不是在下面的tools中運行),則刪除E:\mine\python_workspace\WebSite目錄下的pyc文件。
PyCharm > settings > tools > external tools > +添加
Name: DelPyc
program: $PyInterpreterDirectory$/python Python安裝路徑
Parameters: $ProjectFileDir$/Oth/Utility/DelPyc.py $FileDir$
Work directory: $FileDir$
Note:Parameters後面的 $FileDir$參數是說,DelPyc是針對當前pycharm中打開的py文件對應的目錄刪除其中全部的pyc文件。
Note:再添加一個Tools名爲DelPycIn
program: Python安裝路徑,e.g. D:\python3.4.2\python.exe
Parameters: E:\mine\python_workspace\Utility\DelPyc.py
Work directory 使用變量 $FileDir$
參數中沒有$FileDir$,這樣就能夠直接刪除經常使用目錄r'E:\mine\python_workspace\WebSite'了,兩個一塊兒用更方便
當你在打字的時候,PyCharm會檢查你的代碼是否符合PEP8。它會讓你知道,你是否有太多的空格或空行等等。若是你願意,你能夠配置PyCharm運行pylint做爲外部工具。
/usr/bin/2to3 -wn $FileDir$
這樣在pycharm中打開某個文件,右鍵external tools > py2topy3就能夠瞬間將當前文件所在目錄下的全部py2轉換成py3,是否是很機智!
[python2和python3的區別、轉換及共存 使用 2to3 工具對代碼進行檢查和轉換]
ref:pycharm的一些設置和快捷鍵