标签归档:pycharm

PyCharm中未解决的参考问题

问题:PyCharm中未解决的参考问题

我有一个目录结构

├── simulate.py
├── src
   ├── networkAlgorithm.py
   ├── ...

而且我可以使用访问网络模块sys.path.insert()

import sys
import os.path
sys.path.insert(0, "./src")
from networkAlgorithm import *

但是,pycharm抱怨它无法访问该模块。我如何教pycham解析参考文献?

I have a directory structure

├── simulate.py
├── src
│   ├── networkAlgorithm.py
│   ├── ...

And I can access the network module with sys.path.insert().

import sys
import os.path
sys.path.insert(0, "./src")
from networkAlgorithm import *

However, pycharm complains that it cannot access the module. How can I teach pycham to resolve the reference?


回答 0

手动将其添加为你所做确实这样做的一种方式,但有一个简单的方法,那就是通过简单地告诉pycharm要在添加src文件夹作为源根目录,然后添加源根你的Python路径。

这样,您就不必将代码硬编码到解释器的设置中:

  • src为源内容根:

                           

  • 然后确保将添加源添加到您的 PYTHONPATH

  • 现在将解决进口问题:

                     

这样,您可以将任何所需的内容添加为源根,一切将简单地进行。但是,如果未将其标记为源根,则会出现错误:

                                 

毕竟,别忘了重新启动。在PyCharm菜单中,选择:文件->使缓存无效/重新启动

Manually adding it as you have done is indeed one way of doing this, but there is a simpler method, and that is by simply telling pycharm that you want to add the src folder as a source root, and then adding the sources root to your python path.

This way, you don’t have to hard code things into your interpreter’s settings:

  • Add src as a source content root:

                           

  • Then make sure to add add sources to your PYTHONPATH:

  • Now imports will be resolved:

                     

This way, you can add whatever you want as a source root, and things will simply work. If you unmarked it as a source root however, you will get an error:

                                 

After all this don’t forget to restart. In PyCharm menu select: File –> Invalidate Caches / Restart


回答 1

  1. 检查__init__.py文件src夹文件
  2. 添加 src文件夹为源根
  3. 然后确保将添加源添加到您的 PYTHONPATH(见上文)
  4. 在PyCharm菜单中选择:File-> Invalidate Caches / Restart
  1. check for __init__.py file in src folder
  2. add the src folder as a source root
  3. Then make sure to add add sources to your PYTHONPATH (see above)
  4. in PyCharm menu select: File –> Invalidate Caches / Restart

回答 2

如果有人还在看,我尝试使用PyCharm 2016.3时,可接受的答案仍然有效。用户界面可能已更改,但选项仍然相同。

即。右键单击您的根文件夹->“标记目录为”->源根

If anyone is still looking at this, the accepted answer still works for PyCharm 2016.3 when I tried it. The UI might have changed, but the options are still the same.

ie. Right click on your root folder –> ‘Mark Directory As’ –> Source Root


回答 3

测试完所有变通办法之后,建议您Settings -> Project -> project dependencies重新看看并重新安排它们。

After testing all workarounds, i suggest you to take a look at Settings -> Project -> project dependencies and re-arrange them.


回答 4

通常,$ PYTHONPATH用于教python解释器查找必要的模块。PyCharm需要在“首选项”中添加路径。


回答 5

通常,这是缺少包装的问题,只需将插入号放在未解决的参考上并按Alt+Enter以显示选项,那么您应该知道如何解决。

Generally, this is a missing package problem, just place the caret at the unresolved reference and press Alt+Enter to reveal the options, then you should know how to solve it.


回答 6

遵循接受的答案后,请执行以下操作为我解决此问题:

File→交通Settings→交通Project <your directory/project>→交通Project Dependencies

选择您的未解析导入文件所在的目录/项目,并选中相应的框以告知Pycharm该项目取决于您的其他项目。

我的文件夹层次结构与问题中的文件夹层次结构略有不同。我的就是这样

├── MyDirectory  
     └── simulate.py  
├── src  
     ├── networkAlgorithm.py  
     ├── ...

告诉Pycharm src依赖于MyDirectory我,此问题已解决!

After following the accepted answer, doing the following solved it for me:

FileSettingsProject <your directory/project>Project Dependencies

Chose the directory/project where your file that has unresolved imports resides and check the box to tell Pycharm that that project depends on your other project.

My folder hierarcy is slightly different from the one in the question. Mine is like this

├── MyDirectory  
│     └── simulate.py  
├── src  
│     ├── networkAlgorithm.py  
│     ├── ...

Telling Pycharm that src depends on MyDirectory solved the issue for me!


回答 7

尽管所有答案都确实有帮助,但是应该明确解释其中一小部分信息:

  • 本质上,具有多个分层目录的项目将作为具有某些属性的包来工作。
  • 要导入本地创建的自定义类,我们需要导航到包含.py文件的目录并创建一个__init__.py(空)文件。

之所以有帮助,是因为该文件是使Python将目录视为包含包所必需的。干杯!

Although all the answers are really helpful, there’s one tiny piece of information that should be explained explicitly:

  • Essentially, a project with multiple hierarchical directories work as a package with some attributes.
  • To import custom local created Classes, we need to navigate to the directory containing .py file and create an __init__.py (empty) file there.

Why this helps is because this file is required to make Python treat the directory as containing packages. Cheers!


回答 8

通过PyCharm安装(与Community Edition一起使用)。打开,Settings > Project > Project Interpreter然后单击下面的屏幕快照中的绿色+图标。在打开的第二个对话框中,输入软件包名称,然后单击“安装软件包”按钮。

Install via PyCharm (works with Community Edition). Open up Settings > Project > Project Interpreter then click the green + icon in the screenshot below. In the 2nd dialogue that opens, enter the package name and click the ‘Install Package’ button.


回答 9

很多时候发生的事情是没有安装插件。例如

如果您正在开发django项目,并且在pyCharm中未安装django插件,则会显示错误“未解决的引用”。请参阅:https : //www.jetbrains.com/pycharm/help/resolving-references.html

Many a times what happens is that the plugin is not installed. e.g.

If you are developing a django project and you do not have django plugin installed in pyCharm, it says error ‘unresolved reference’. Refer: https://www.jetbrains.com/pycharm/help/resolving-references.html


回答 10

请检查您是否使用了正确的解释器。我遇到错误“无法解析的参考’django’”以解决此问题,我从项目设置中更改了项目解释器(将Python 3更改为Python 2.7):选择项目,转到文件->设置->项目:->项目解释器->浏览并选择正确的版本或解释器(例如/usr/bin/python2.7)。

Please check if you are using the right interpreter that you are supposed to. I was getting error “unresolved reference ‘django’ ” to solve this I changed Project Interpreter (Changed Python 3 to Python 2.7) from project settings: Select Project, go to File -> Settings -> Project: -> Project Interpreter -> Brows and Select correct version or Interpreter (e.g /usr/bin/python2.7).


回答 11

就我而言,问题是我正在使用 Virtual environment是无法访问全局站点程序包的。因此,解释器不知道新安装的软件包。

要解决此问题,只需编辑或重新创建您的虚拟解释器,然后勾选该Inherit global site-packages选项。

In my case the problem was I was using Virtual environment which didn’t have access to global site-packages. Thus, the interpreter was not aware of the newly installed packages.

To resolve the issue, just edit or recreate your virtual interpreter and tick the Inherit global site-packages option.


回答 12

这为我工作: Top Menu -> File -> Invalidate Caches/Restart

This worked for me: Top Menu -> File -> Invalidate Caches/Restart


回答 13

在PyCharm 2019.3.1中完成。 右键单击src文件夹->“标记目录为”->单击“排除”,您的src文件夹应为蓝色。

Done in PyCharm 2019.3.1 Right-click on your src folder -> “Mark Directory as” -> Click-on “Excluded” and your src folder should be blue.


回答 14

Pycharm使用venv。在venv的控制台中,您应该显式安装软件包或进入settings -> project interpreter -> add interpreter -> inherit global site-packages

Pycharm uses venv. In the venv’s console you should install the packages explicitly or go in settings -> project interpreter -> add interpreter -> inherit global site-packages.


回答 15

在较新版本的pycharm中,您可以简单地执行以下操作:右键单击要从中导入文件的目录或python包,然后单击“将目录标记为”->“源根”

In newer versions of pycharm u can do simply by right clicking on the directory or python package from which you want to import a file, then click on ‘Mark Directory As’ -> ‘Sources Root’


回答 16

解决此问题的最简单方法是在pyCharm软件中执行以下操作:

单击:文件>设置>(项目:您的项目名称)>项目解释器>

然后单击右侧的“ +”图标以搜索所需的软件包并安装。

享受编码!

The easiest way to fix it is by doing the following in your pyCharm software:

Click on: File > Settings > (Project: your project name) > Project Interpreter >

then click on the “+” icon on the right side to search for the package you want and install it.

Enjoy coding !!!


如何指定方法的返回类型与类本身相同?

问题:如何指定方法的返回类型与类本身相同?

我在python 3中有以下代码:

class Position:

    def __init__(self, x: int, y: int):
        self.x = x
        self.y = y

    def __add__(self, other: Position) -> Position:
        return Position(self.x + other.x, self.y + other.y)

但是我的编辑器(PyCharm)说,参考位置无法解析(在__add__方法中)。我应该如何指定期望返回类型为type Position

编辑:我认为这实际上是一个PyCharm问题。它实际上在警告和代码完成中使用该信息

但如果我错了,请纠正我,并需要使用其他语法。

I have the following code in python 3:

class Position:

    def __init__(self, x: int, y: int):
        self.x = x
        self.y = y

    def __add__(self, other: Position) -> Position:
        return Position(self.x + other.x, self.y + other.y)

But my editor (PyCharm) says that the reference Position can not be resolved (in the __add__ method). How should I specify that I expect the return type to be of type Position?

Edit: I think this is actually a PyCharm issue. It actually uses the information in its warnings, and code completion

But correct me if I’m wrong, and need to use some other syntax.


回答 0

TL; DR:如果您使用的是Python 4.0,它将正常工作。从今天(2019年)开始,在3.7+中,您必须使用将来的语句(from __future__ import annotations)启用此功能-对于Python 3.6或更低版本,请使用字符串。

我猜你有这个exceptions:

NameError: name 'Position' is not defined

这是因为Position必须先定义,然后才能在批注中使用它,除非您正在使用Python 4。

Python 3.7+: from __future__ import annotations

Python 3.7引入了PEP 563:推迟对注释的评估。使用future语句的模块from __future__ import annotations将自动将注释存储为字符串:

from __future__ import annotations

class Position:
    def __add__(self, other: Position) -> Position:
        ...

按计划,它将成为Python 4.0中的默认设置。由于Python仍然是一种动态类型化的语言,因此在运行时不进行类型检查,因此键入注释应该不会对性能产生影响,对吗?错误!在python 3.7之前,键入模块曾经是内核中最慢的python模块之一,因此,如果升级到3.7,import typing您将看到性能提高多达7倍

Python <3.7:使用字符串

根据PEP 484,您应该使用字符串而不是类本身:

class Position:
    ...
    def __add__(self, other: 'Position') -> 'Position':
       ...

如果您使用Django框架,可能会很熟悉,因为Django模型还将字符串用于正向引用(外键模型已self声明或尚未声明的外键定义)。这应该与Pycharm和其他工具一起使用。

资料来源

PEP 484PEP 563的相关部分,为您节省行程:

转发参考

当类型提示包含尚未定义的名称时,该定义可以表示为字符串文字,稍后再解析。

通常会发生这种情况的情况是容器类的定义,其中定义的类出现在某些方法的签名中。例如,以下代码(简单的二叉树实现的开始)不起作用:

class Tree:
    def __init__(self, left: Tree, right: Tree):
        self.left = left
        self.right = right

为了解决这个问题,我们写:

class Tree:
    def __init__(self, left: 'Tree', right: 'Tree'):
        self.left = left
        self.right = right

字符串文字应包含有效的Python表达式(即,compile(lit,”,’eval’)应为有效的代码对象),并且在模块完全加载后,其值应无错误。在其中评估本地和全局命名空间的命名空间应与在其中评估同一函数的默认参数的命名空间相同。

和PEP 563:

在Python 4.0中,将不再在定义时评估函数和变量注释。而是将字符串形式保留在相应的__annotations__字典中。静态类型检查器在行为上不会有任何区别,而在运行时使用批注的工具将必须执行推迟的评估。

可以使用以下特殊导入从Python 3.7开始启用上述功能:

from __future__ import annotations

您可能会想做的事情

A.定义一个假人 Position

在类定义之前,放置一个虚拟定义:

class Position(object):
    pass


class Position(object):
    ...

这样可以摆脱NameError甚至看起来还可以:

>>> Position.__add__.__annotations__
{'other': __main__.Position, 'return': __main__.Position}

但是吗?

>>> for k, v in Position.__add__.__annotations__.items():
...     print(k, 'is Position:', v is Position)                                                                                                                                                                                                                  
return is Position: False
other is Position: False

B. Monkey-patch为了添加注释:

您可能想尝试一些Python元编程魔术,并编写装饰器以Monkey修补类定义,以便添加注释:

class Position:
    ...
    def __add__(self, other):
        return self.__class__(self.x + other.x, self.y + other.y)

装饰者应对此负责:

Position.__add__.__annotations__['return'] = Position
Position.__add__.__annotations__['other'] = Position

至少看起来是正确的:

>>> for k, v in Position.__add__.__annotations__.items():
...     print(k, 'is Position:', v is Position)                                                                                                                                                                                                                  
return is Position: True
other is Position: True

可能麻烦太多了。

结论

如果您使用的是3.6或更低版本,请使用包含类名的字符串文字,在3.7中使用from __future__ import annotations它就可以了。

TL;DR: if you are using Python 4.0 it just works. As of today (2019) in 3.7+ you must turn this feature on using a future statement (from __future__ import annotations) – for Python 3.6 or below use a string.

I guess you got this exception:

NameError: name 'Position' is not defined

This is because Position must be defined before you can use it in an annotation unless you are using Python 4.

Python 3.7+: from __future__ import annotations

Python 3.7 introduces PEP 563: postponed evaluation of annotations. A module that uses the future statement from __future__ import annotations will store annotations as strings automatically:

from __future__ import annotations

class Position:
    def __add__(self, other: Position) -> Position:
        ...

This is scheduled to become the default in Python 4.0. Since Python still is a dynamically typed language so no type checking is done at runtime, typing annotations should have no performance impact, right? Wrong! Before python 3.7 the typing module used to be one of the slowest python modules in core so if you import typing you will see up to 7 times increase in performance when you upgrade to 3.7.

Python <3.7: use a string

According to PEP 484, you should use a string instead of the class itself:

class Position:
    ...
    def __add__(self, other: 'Position') -> 'Position':
       ...

If you use the Django framework this may be familiar as Django models also use strings for forward references (foreign key definitions where the foreign model is self or is not declared yet). This should work with Pycharm and other tools.

Sources

The relevant parts of PEP 484 and PEP 563, to spare you the trip:

Forward references

When a type hint contains names that have not been defined yet, that definition may be expressed as a string literal, to be resolved later.

A situation where this occurs commonly is the definition of a container class, where the class being defined occurs in the signature of some of the methods. For example, the following code (the start of a simple binary tree implementation) does not work:

class Tree:
    def __init__(self, left: Tree, right: Tree):
        self.left = left
        self.right = right

To address this, we write:

class Tree:
    def __init__(self, left: 'Tree', right: 'Tree'):
        self.left = left
        self.right = right

The string literal should contain a valid Python expression (i.e., compile(lit, ”, ‘eval’) should be a valid code object) and it should evaluate without errors once the module has been fully loaded. The local and global namespace in which it is evaluated should be the same namespaces in which default arguments to the same function would be evaluated.

and PEP 563:

In Python 4.0, function and variable annotations will no longer be evaluated at definition time. Instead, a string form will be preserved in the respective __annotations__ dictionary. Static type checkers will see no difference in behavior, whereas tools using annotations at runtime will have to perform postponed evaluation.

The functionality described above can be enabled starting from Python 3.7 using the following special import:

from __future__ import annotations

Things that you may be tempted to do instead

A. Define a dummy Position

Before the class definition, place a dummy definition:

class Position(object):
    pass


class Position(object):
    ...

This will get rid of the NameError and may even look OK:

>>> Position.__add__.__annotations__
{'other': __main__.Position, 'return': __main__.Position}

But is it?

>>> for k, v in Position.__add__.__annotations__.items():
...     print(k, 'is Position:', v is Position)                                                                                                                                                                                                                  
return is Position: False
other is Position: False

B. Monkey-patch in order to add the annotations:

You may want to try some Python meta programming magic and write a decorator to monkey-patch the class definition in order to add annotations:

class Position:
    ...
    def __add__(self, other):
        return self.__class__(self.x + other.x, self.y + other.y)

The decorator should be responsible for the equivalent of this:

Position.__add__.__annotations__['return'] = Position
Position.__add__.__annotations__['other'] = Position

At least it seems right:

>>> for k, v in Position.__add__.__annotations__.items():
...     print(k, 'is Position:', v is Position)                                                                                                                                                                                                                  
return is Position: True
other is Position: True

Probably too much trouble.

Conclusion

If you are using 3.6 or below use a string literal containing the class name, in 3.7 use from __future__ import annotations and it will just work.


回答 1

将类型指定为字符串是可以的,但总是让我有些讨厌,因为我们基本上是在绕过解析器。因此,您最好不要拼写以下任何文字字符串:

def __add__(self, other: 'Position') -> 'Position':
    return Position(self.x + other.x, self.y + other.y)

有一个细微的变化是使用绑定的typevar,至少在声明typevar时,您只需编写一次字符串即可:

from typing import TypeVar

T = TypeVar('T', bound='Position')

class Position:

    def __init__(self, x: int, y: int):
        self.x = x
        self.y = y

    def __add__(self, other: T) -> T:
        return Position(self.x + other.x, self.y + other.y)

Specifying the type as string is fine, but always grates me a bit that we are basically circumventing the parser. So you better not misspell any one of these literal strings:

def __add__(self, other: 'Position') -> 'Position':
    return Position(self.x + other.x, self.y + other.y)

A slight variation is to use a bound typevar, at least then you have to write the string only once when declaring the typevar:

from typing import TypeVar

T = TypeVar('T', bound='Position')

class Position:

    def __init__(self, x: int, y: int):
        self.x = x
        self.y = y

    def __add__(self, other: T) -> T:
        return Position(self.x + other.x, self.y + other.y)

回答 2

在解析类主体本身时,名称“ Position”不可用。我不知道您如何使用类型声明,但是Python的PEP 484-如果使用这些键入提示表示您可以在此时将名称简单地作为字符串,这是大多数模式应使用的方式:

def __add__(self, other: 'Position') -> 'Position':
    return Position(self.x + other.x, self.y + other.y)

检查https://www.python.org/dev/peps/pep-0484/#forward-references-符合该要求的工具将知道从那里解包并使用类名。(请记住,Python语言本身不执行任何这些注释-它们通常用于静态代码分析,或者可以具有一个库/框架以在运行时进行类型检查-但您必须明确地进行设置。

更新此外,从Python 3.8开始,请检查pep-563-从Python 3.8开始,可以编写from __future__ import annotations以推迟对批注的求值-前向引用类应简单易用。

The name ‘Position’ is not avalilable at the time the class body itself is parsed. I don’t know how you are using the type declarations, but Python’s PEP 484 – which is what most mode should use if using these typing hints say that you can simply put the name as a string at this point:

def __add__(self, other: 'Position') -> 'Position':
    return Position(self.x + other.x, self.y + other.y)

Check https://www.python.org/dev/peps/pep-0484/#forward-references – tools conforming to that will know to unwrap the class name from there and make use of it.(It is always important to have in mind that the Python language itself does nothing of these annotations – they are usually meant for static-code analysis, or one could have a library/framework for type checking in run-time – but you have to explicitly set that).

update Also, as of Python 3.8, check pep-563 – as of Python 3.8 it is possible to write from __future__ import annotations to defer the evaluation of annotations – forward referencing classes should work straightforward.


回答 3

当基于字符串的类型提示可接受时,__qualname__也可以使用该项目。它包含类的名称,并且在类定义的主体中可用。

class MyClass:
    @classmethod
    def make_new(cls) -> __qualname__:
        return cls()

这样,重命名类并不意味着修改类型提示。但是我个人并不希望智能代码编辑器能够很好地处理这种形式。

When a string-based type hint is acceptable, the __qualname__ item can also be used. It holds the name of the class, and it is available in the body of the class definition.

class MyClass:
    @classmethod
    def make_new(cls) -> __qualname__:
        return cls()

By doing this, renaming the class does not imply modifying the type hints. But I personally would not expect smart code editors to handle this form well.


Pycharm+Django 安装及配置指南

虽然平时我极力推荐使用简单、轻便、灵活的VSCode编辑器进行开发。

但是对于大型Web项目(Django、Tornado等框架)开发,我还是推荐大家使用Pycharm.

Pycharm拥有强大的配置工具、Git版本管理工具、代码补全工具、Debug工具等等,这些都是进行大型项目开发的利器。

尤其是今天的主角Django,由于太过于重要了,Pycharm甚至专门给其提供了配置模板:

能直接在新建项目的时候选择Django并新建一个独立的虚拟环境:

从新建到编码测试,一套流程用起来都极其方便。

1.下载Pycharm

在jetbrains官网选择相应的系统下载Pycharm:
https://www.jetbrains.com/pycharm/download/

这里强烈推荐下载Professional版(专业版),激活方法可以自行在网上查询,推荐知了哥的文章(zhile.io)。

安装完成后,根据你是否需要新建Django项目分为两种配置方式。

1.1 新建Django项目

File-New Project 新建一个Django项目:

强烈推荐新建一个环境,默认新建环境的工具是Virtualenv, 我这里用的是conda,效果其实都差不多。区别在于,conda可以选择Python版本

输入好location(安装位置)后点击create,即可生成Django项目。

1.2 配置已开发的Django项目

Pycharm中适配已开发的Django项目也非常容易,因为它为这种情况专门提供了配置模板:

点击右上角的配置框选择 Edit Configurations

进来后先输入Name 项目名称,然后在Python Interpreter选择你的代码所属环境的编译器,最后选择Fix,弹出Django配置页。

在点击Fix后出现的配置页中,输入这三项:

第一个是项目根目录,第二个是settings.py文件的位置,第三个是manage.py的位置。三者缺一不可。搞定后点击OK,配置完成。

2.运行项目

Pycharm运行Django项目只需要点击右上角这两者之一即可:

第一个是普通的启动方式,第二个是Debug启动方式,推荐第二个,因为开发的时候如果需要跟踪代码流程,Debug模式非常方便。

点击后会自动生成启动Django的命令,你可以在console里查看该语句,出现以下的输出即启动完成:

访问http://127.0.0.1:8000/就是网站的首页了。

这里默认使用的端口号是8000,你可以在配置页修改默认的域名和端口,只不过其他域名你需要在hosts中将其定向到127.0.0.1, 比如:

修改hosts文件,增加:dev.goldenstone.com 127.0.0.1

保存后修改右上角的configurations配置:

在settings.py中,将dev.goldenstone.com这个域名加入到 ALLOWED_HOSTS 中:

这样就可以通过 http://dev.goldenstone.com:5555/ 访问你的开发环境了:

3.工具

下面介绍一些Pycharm中独有的、特别的工具:

3.1 查看文件历史修改及提交记录非常方便:

点击每一个提交,都能看到每次提交的内容。

3.2 选择指定的commit行

VCS-Commit,它可以自由选择你需要提交的代码块:

3.3 自动格式化代码

3.4 全局搜索

尽管VSCode也有全局搜索,但相信我,他们两个不是一个概念:

windows下选择 Ctrl+shift+F 即可在Pycharm中全局搜索,或者在Edit-Find-Find in Path 找到该功能:

界面如下,它不仅仅是全局搜索,还能指定模块、目录进行搜索。

指定文件名搜索,高级过滤器中还能搜索指定除注释以外的符合关键词的句子等等,是一个非常强大的搜索工具。

这个搜索工具对于我而言,使用频率仅次于Debug工具。


Pycharm中非常有用的生产工具还有很多,简直是用都用不完,下次再给大家详细介绍一番!

我们的文章到此就结束啦,如果你喜欢今天的Python 实战教程,请持续关注Python实用宝典。

有任何问题,可以在公众号后台回复:加群,回答相应验证信息,进入互助群询问。

原创不易,希望你能在下面点个赞和在看支持我继续创作,谢谢!


​Python实用宝典 (pythondict.com)
不只是一个宝典
欢迎关注公众号:Python实用宝典

这些 PyCharm 高效操作你知道吗?

熟话说,工欲善其事,必先利其器,PyCharm 作为最好用的 IDE 工具,有着各种各样的骚操作,这是高级开发工程师必须熟悉的基本技能。

pycharm 小技巧是很多的,今天就给大家推荐一些看似简单确非常高效的操作。让你脱离 CV 工程师走向更高一级台阶。

0x01 更换背景图

哈,每天的工作就是面对一堆代码有时候也是会觉得挺烦心的是吧,特别是找 bug 找好久依旧定位不到问题的时候,这时候就体现出男女搭配干活不累的好处了,通过更换 IDE 的背景图让你的程序员鼓励师每天不重样。

设置路径如下:Preferences -> Appearance -> Background Image

至于去哪里找程序员鼓励师的高清图片,我相信你们知道的肯定比我多,对吧。

0x02 打开最近打开的文件

有时候打开的文件实在是太多了,tab 栏就会被占满,想定位到自己想要的文件还是很麻烦的,这时候这个快捷键操作就派上用场了,「command + E」,可以直接打开自己最近编辑过的文件,方便快捷,按两下有惊喜哦。

0x03 查看源码

有时候代码一多,看起来很乱,根本不晓得这个变量是在哪里定义的,用来干嘛的,这时候就可以通过「command + B」来快速定位到定义变量的位置,亦或者在将光标定位到函数位置,按「command + B」可以直达函数的内部实现,看开源代码时特别方便。

0x04 开启新的一行

写程序我们经常需要换行,这都是常规操作,如果你还是先将光标定位到行尾或者行首,之后按 Enter 键来换行,效率就有点低了。如论你的光标在一行的哪个位置,都可以通过「shift enter」在本行后面开启新的一行,通过「command + option + enter」在本行前面开启新的一行。

0x05 快速返回

这个我一般会搭配「command + E」来一起使用,如果你想去的文件是刚编辑过的,那么可以直接使用 「command + option + 方向箭」来快速跳转到刚才编辑的位置,不仅仅局限于不同文件哦,在同一个文件内也是可以的。

0x06 查找类/文件

当你想快速搜索一个类或者文件时,可以双击 shift,直接输入你要搜索的内容即可。同时如果你确定搜索的是类的话,那么可以通过「command + o」来开启精准搜索,如果搜索的是文件,那么可以通过「command + shift + o」精准搜索。

0x07 自动清除没有引用的包

有时候看到那些变灰色的 import 很是烦人,难道只能一个一个删除么,当然不是,直接 「command + option + o」即可一键清除。

总结

今天给大家普及了一些 PyCharm 的高效操作,希望对小伙伴们的工作和学习有所帮助。