新手,刚学了2天Python 发现很有趣,有个问题想问下
如图:d.py在ssffe模块module中 c.py在一个名字为tesat的文件夹directory中
临时建立的,名字随便打的别介意。。
在d.py中
print 'dddd'
在c.py中
from ssffe import d
print 'cccc'
执行c.py 输出:
dddd
cccc
问题:我想在d.py中引用 c.py
发现用语句 from tesat import c 程序报错
即导入系统中其他目录的Python文件
导入语句怎么写
写了很多仍然报错
Directory is a directory. Pycharm will think that you may store some of your resources in it, such as css/js, or some configuration files. There is no __init__.py in it, and package will create __init__.py. This relates to which of your python files are visible to the outside world.
Or you can understand it this way, the visibility of python at the file level or class name level is represented by __2 underscores. For the visibility of the file, it is described by __init__.py defined in the package
A is the Directory you mentioned
B is a package with __init__.py in it
If you don’t want to add __init__.py to A to turn it into a package
Manually add the directory of A dynamically in the code In sys.path
In this way, when python imports modules, the specified modules will be loaded in the order of the sys.path list
In this way, when you import foo from API, you will find the python file under A
API.py implementation
No one answered?
Directory is a normal directory; package is a directory with an __init__.py file, which is related to python’s import mechanism.
It is recommended to take a look at the python tutorial Chapter 6 Modules (available after installation, or go to the official website, if you don’t want to read it in English) (Chinese python documentation).