如何在pycharm项目中导入位于同一子目录中的python文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35950050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:11:26  来源:igfitidea点击:

How to import python file located in same subdirectory in a pycharm project

pythonpycharmimporterror

提问by Christopher

I have an input error in pycharm when debugging and running.

我在调试和运行时在 pycharm 中出现输入错误。

My project structure is rooted properly, etc./HW3/.so that HW3is the root directory.

我的项目结构是正确的,etc./HW3/.所以这HW3是根目录。

I have a subfolder in HW3, util, and a file, util/util.py. I have another file in utilcalled run_tests.py.

我有一个子文件夹在HW3,util以及文件util/util.py。我有另一个util名为run_tests.py.

In run_tests.py, I have the following import structure,

run_tests.py,我有以下导入结构,

from util.util import my_functions, etc.

This yields an input error, from util.util import load_dataset,proportionate_sample ImportError: No module named 'util.util'; 'util' is not a package

这会产生输入错误, from util.util import load_dataset,proportionate_sample ImportError: No module named 'util.util'; 'util' is not a package



However, in the exact same project, in another directory (same level as util) called data, I have a file data/data_prep.py, which also imports functions from util/util.pyusing a similar import statement...and it runs without any problems.

但是,在完全相同的项目中,在另一个util名为 的目录(与 相同级别)中data,我有一个文件data/data_prep.py,它也util/util.py使用类似的导入语句导入函数......并且它运行没有任何问题。



Obviously, I am doing this in the course of doing a homework, so please understand: this is ancillary to the scope of the homework.

显然,我是在做作业的过程中做这个的,所以请理解:这是作业范围的附属。



The problem goes away when I move the file to another directory. So I guess this question is How do I import a python file located in the same directory in a pycharm project?Because pycharm raises an error if I just do import utiland prompts me to use the full name from the root.

当我将文件移动到另一个目录时,问题就消失了。所以我想这个问题是如何在pycharm项目中导入位于同一目录中的python文件?因为如果我这样做import util,pycharm 会引发错误并提示我使用根目录中的全名。

采纳答案by danidee

If you don't have an __init__.pycreate one and add this line

如果您没有__init__.py创建并添加这一行

from util.util import my_function

then you can easily import the module in your scripts the __init__.pytells python that it should treat that folder as a python package, it can also be used to import/load modules too.

然后您可以轻松地在脚本中导入模块,__init__.py告诉 python 它应该将该文件夹视为一个 python 包,它也可以用于导入/加载模块。

in most cases the __init__.pyis empty.

大多数情况下__init__.py是空的。

Quoting the docs

引用文档

The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.

需要 __init__.py 文件才能使 Python 将目录视为包含包;这样做是为了防止具有通用名称(例如字符串)的目录无意中隐藏稍后出现在模块搜索路径上的有效模块。在最简单的情况下,__init__.py 可以只是一个空文件,但它也可以执行包的初始化代码或设置 __all__ 变量,稍后描述。

回答by imanzabet

Recommended Way:

推荐方式:

Make sure to set the working folder as Sources.

确保将工作文件夹设置为Sources.

You can do it in Pycharm->Preferences->Project: XYZ->Project Structure

你可以在 Pycharm->Preferences->Project: XYZ->Project Structure

Select your working folder and mark it as Sources. Then Pycharm recognize the working folder as a Source folder for the project and you will be able to simply add other files within that folder by using

选择您的工作文件夹并将其标记为Sources. 然后 Pycharm 将工作文件夹识别为项目的源文件夹,您将能够通过使用简单地在该文件夹中添加其他文件

import filename.py

or

或者

from filename.py import mudule1

=================

==================

Not recommended way:

不推荐的方式:

In Pycharmyou can simply add .before the .pyfile which you are going to import it from the same folder. In your case it will be

Pycharm你可以简单地添加.在之前.py您打算从导入文件相同的文件夹。在你的情况下,它将是

from .util import my_functions

回答by Srikar Durgi

Right-click on the folder which you want to be marked as the source > Mark Directory as > Source root.

右键单击要标记为源的文件夹 > 将目录标记为 > 源根。

回答by lfvv

In my case, it worked only when I omit the extension. Example:

就我而言,它仅在我省略扩展名时才有效。例子:

import filename

回答by imsrgadich

Note: May be a bit unrelated.

注意:可能有点无关。

I was facing the same issue but I was unable to import a module in the same directory (rather than subdirectory as asked by OP) when running a jupyter notebook(here the directory didn't have __init__.py). Strangely, I had setup python path and interpreter location and everything. None of the other answers helped but changing the directory in python did.

我遇到了同样的问题,但是在运行jupyter notebook时我无法在同一目录(而不是 OP 要求的子目录)中导入模块(这里的目录没有 __init__.py)。奇怪的是,我已经设置了 python 路径和解释器位置等等。其他答案都没有帮助,但更改了 python 中的目录。

import os
os.chdir(/path/to/your/directory/)

I'm using PyCharm 2017.3on Ubuntu 16.04

我使用PyCharm 2017.3Ubuntu 16.04

回答by Odysseus Ithaca

I had the same issue with pycharm, but the actual mistake was that the file I was trying to import didn't have a .py extension, even though I was able to run it as a standalone script. Look in the explorer window and make sure it has a .py extension. If not, right click on the file in the explorer window, pick refactor, and then rename it with a .py extension.

我在 pycharm 上遇到了同样的问题,但实际的错误是我尝试导入的文件没有 .py 扩展名,即使我能够将它作为独立脚本运行。查看资源管理器窗口并确保它具有 .py 扩展名。如果没有,请在资源管理器窗口中右键单击该文件,选择重构,然后将其重命名为 .py 扩展名。

回答by andrupo

In Pycharm go to "Run - Configuration" and uncheck 'Add Content root to Pythonpath' and 'Add source roots to Pythonpath', then use

在 Pycharm 中,转到“运行 - 配置”并取消选中“将内容根添加到 Pythonpath”和“将源根添加到 Pythonpath”,然后使用

from filename import functionname