PyCharm:引入自定义包报错及解决办法

目录

1.Dictionary 目录及Package包区别

2.Pycharm目录下的包不能被引用解决办法

3.Pycharm 第三方包的引用


1.Dictionary 目录及Package包区别

Dictionary:在pycharm中就是一个文件夹,放置资源文件,对应于在进行JavaWeb开发时用于放置css/js文件的目录,或者说在进行物体识别时,用来存储背景图像的文件夹。该文件夹其中并不包含_ _ init.py_ _文件

Python package :与Dictionary不同之处在于其会自动创建_ _ init__.py文件。 简单的说,python package就是一个目录,其中包括一组模块和一个__init__.py文件。

该文件与Python的import机制有关,这关乎到你的哪些.py文件是对外可访问的。有些时候,如果一个包下有很多模块,在import如此多模块是很费事,且不优雅的,此时可以通过修改_ _ init_ _.py来完成该任务。 
在__init__ .py中定义特殊变量_all_ ,将要包含的模块复制给该变量,例如在 __ init__ .py中定义 _all_=[‘tiff’,’bmp’,’jpg’],这里的all 对应的就是 from …import * 中*代指的模块,此时在引用方使用如下语句:

from image import *
tool = tiff.read('a.tiff')
 


2.Pycharm目录下的包不能被引用解决办法

方法1:使用from…import添加非python的包时, 需要为python解释器指定搜索路径, 比如使用sys.path.insert(), 还可以指定PYTHONPATH, 我用的是PYTHONPATH, 编辑run的配置文件时突然看到"Add source roots to PYTHONPATH", 又突然联想到Mark Directory as, 于是就觉得把文件夹Mark Directory as成root就不用手动添加PYTHONPATH了, 

方法2:(原理同1)​​​​​​​:同包引用错误:右键包 -->Mark Directory as --> Sources Root ,将文件设置为资源引用

Mark Directory as中几个不同选项

Within a content root, PyCharm can distinguish between the folders that contain source code, and the ones to be ignored while searching, parsing, watching and so on. To distinguish different types of folders, mark any folder below a content root as a source folder or as excluded.

      在内容根目录中,PyCharm可以区分包含源代码的文件夹和在搜索、解析、监视等过程中要忽略的文件夹。若要区分不同类型的文件夹,请将内容根目录下的任何文件夹标记为源文件夹或排除文件夹。

  • Source roots contain the actual source files and resources. PyCharm uses the source roots as the starting point for resolving imports

源根包含实际的源文件和资源。PyCharm使用源根作为解析导入的起点

  • Resource roots are intended for resource files in your application (images, Style Sheets, and so on). By assigning a folder to this category, you tell PyCharm that files in it and in its subfolders can be referenced relative to this folder instead of specifying full paths to them.

资源根用于应用程序中的资源文件(图像、样式表等)。通过将文件夹分配给该类别,您可以告诉PyCharm,它及其子文件夹中的文件可以相对于该文件夹被引用,而不是指定指向它们的完整路径。

  • Template roots  contain templates for the various web projects.

模板根包含各种web项目的模板。

  • Excluded roots the Excluded root icon contain files and folders ignored by PyCharm when indexing, searching, parsing, watching, and so on.

排除的根包含PyCharm在索引、搜索、解析、监视等时忽略的文件和文件夹。

3.Pycharm 第三方包的引用

如果是在pycharm 中安装了第三方包如果应用,不是借助框架引入还要在python3.6/site-packages  文件下  pip install ---