从同一文件夹导入时 PyCharm 未解析的引用 [英] PyCharm unresolved reference when importing from the same folder

查看:52
本文介绍了从同一文件夹导入时 PyCharm 未解析的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我刚刚创建一个新项目时,我在项目文件夹我的项目"中有两个 py 文件 - main.py 和 functions.py.在 ma​​in.py 中,我导入了 functions.py 并且运行良好.但是后来我在我的项目"文件夹中创建了一些文件夹,并将 ma​​in.pyfunctions.py 放在其中一个文件夹中.它们仍然在同一个文件夹中,但在 ma​​in.py 中它说 functions.py 是一个未解析的引用.

When I just created a new project I had two py files - main.py and functions.py - in the project folder "my project". In main.py I imported functions.py and it worked fine. But then I created some folders in "my project" folder and put main.py and functions.py in one of them. They are still in the same folder, but in main.py it says that functions.py is an unresolved reference.

附言在 macOS 上使用 PyCharm

P.S. Using PyCharm on macos

推荐答案

简单的答案:你应该在你的目录树中创建一个包,PyCharm 会很容易地提取它.

Simple answer: you should make a package in your directory tree and PyCharm will pick it up easily.

project_root
├── my_project
│   ├── __init__.py
│   ├── main.py
│   ├── functions.py

在 PyCharm 中打开 project_root,这些导入应该可以正常工作:

Open project_root in PyCharm and these imports should work correctly:

from my_project import functions

import my_project.functions
# you can also do 'import my_project.functions as functions'

my_project.functions.do_something('argument')

这篇关于从同一文件夹导入时 PyCharm 未解析的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆