Python 编译py文件时没有名为utils的模块错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27064348/
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 01:23:41  来源:igfitidea点击:

No module named utils error on compiling py file

pythonweb.pypython-3.4

提问by Andrew Brick

I'm trying to run a .pyfile through the command prompt using the command "python filename.py". I've already set the environment variables for python after I installed it, so I don't get any error when I type python. The file I'm running imports a few directories, all of which are preexistent in the same directory as the file I'm running, apart from the file web.py, which I can't seem to locate in the directory, so I'm assuming it's somewhere inside the python package, I have downloaded. But, I couldn't find it there either, so would I need to install an extension for python for the web.py file to be successfully imported or is there another way around this.

我正在尝试.py使用命令“python filename.py”通过命令提示符运行文件。安装python后我已经设置好了环境变量,所以输入python的时候没有报错。我正在运行的文件导入了几个目录,所有这些目录都与我正在运行的文件位于同一目录中,除了文件 web.py,我似乎无法在目录中找到它,所以我假设它在 python 包内的某个地方,我已经下载了。但是,我在那里也找不到它,所以我需要为 web.py 文件安装 python 扩展程序才能成功导入,还是有其他方法可以解决这个问题。

I've downloaded Python 3.4, I'm using windows 7 as my operating system and the exact error I receive when I try to compile the file is

我已经下载了 Python 3.4,我使用 Windows 7 作为我的操作系统,当我尝试编译文件时收到的确切错误是

ImportError: No module named 'utils'

Can someone please explain or direct me to a page which shows in detail how to install extensions for python?

有人可以解释或引导我到一个详细显示如何安装 python 扩展的页面吗?

采纳答案by Thorin Schmidt

The specific error happens when the Python interpreter can't find a particular ".py" file. In your case, it is the file "utils.py".

当 Python 解释器找不到特定的“.py”文件时,就会发生特定错误。在您的情况下,它是文件“utils.py”。

First you need to find which file is trying to import "utils.py". Starting with your main file, look up all the files you are importing. (I am guessing this issue is coming from one of the non-library files, but I could be wrong.)

首先,您需要找到正在尝试导入“utils.py”的文件。从您的主文件开始,查找您要导入的所有文件。(我猜这个问题来自非库文件之一,但我可能是错的。)

Once you have the "top level" import list, check each of those files to see what THEY are importing, and repeat the process for them. Eventually, you will find the .py file which is trying to import "utils". There might be a directory specification forcing Python to look in the wrong place.

拥有“顶级”导入列表后,检查每个文件以查看它们正在导入的内容,并为它们重复该过程。最终,您将找到试图导入“utils”的 .py 文件。可能存在强制 Python 查找错误位置的目录规范。

Finally, using windows' file manager, perform a search for "utils.py". As a temporary fix, you can copy it from its current location into your working directory. That will at least allow you to get your project up and running until you sort out the real cause.

最后,使用 Windows 的文件管理器,搜索“utils.py”。作为临时修复,您可以将其从当前位置复制到您的工作目录中。这至少可以让您启动并运行您的项目,直到您找出真正的原因。

回答by webWizard

This error occurs due to file(s)/folder(s) that are not in their respective locations.

由于文件/文件夹不在各自的位置而发生此错误。

I had a very similar error with a Python Flask Framework app, it turns out that my manage.py and config.py files were inside the app folder with the other folders(they were supposed to be outside the app directory), and that cause the error in my situation.

我在 Python Flask Framework 应用程序中遇到了非常相似的错误,结果是我的 manage.py 和 config.py 文件与其他文件夹位于 app 文件夹内(它们应该位于 app 目录之外),这导致我的情况的错误。

Once I placed the files in their proper location boom error was gone.

一旦我将文件放在适当的位置,繁荣错误就消失了。

So Check you application framework and make sure things are located were they're supposed to be. Good luck

因此,请检查您的应用程序框架,并确保事物已定位到它们应有的位置。祝你好运

回答by Mukund Parmar

So in my case I ran tree command in my Pipenv environment and it should be look like as below: I hope this helps.

所以就我而言,我在我的 Pipenv 环境中运行了 tree 命令,它应该如下所示:我希望这会有所帮助。

.
├── README.md
├── __init__.py
├── core.yaml
├── core_blueprints
│   ├── __init__.py
│   ├── ami_lookup.py
│   ├── chef_buckets.py
│   ├── custom_resources
│   │   ├── __init__.py
│   │   └── cfn_custom_classes.py
│   ├── cw_alarm.py
│   ├── roles.py
│   ├── security_groups.py
│   ├── shared_iam
│   │   ├── __init__.py
│   │   └── iam_policies.py
│   ├── sns_subscription.py
│   ├── sns_topic.py
│   ├── ssm_chefrun_documents.py
│   ├── tf_state.py
│   ├── utils .                 #### This is not correct location.
│   │   ├── __init__.py
│   │   ├── standalone_output.py
│   │   ├── version.py
│   │   └── version_check.py
│   ├── vpc.py
│   ├── vpn_eip.py
│   └── vpn_server.py
├── core_hooks
│   ├── __init__.py
│   ├── cookbook_archive.py
│   ├── core_lambda.py
│   ├── keypair.py
│   ├── s3.py
│   ├── s3_cache.py
│   └── ssm.py
├── platform_version.py
├── prd1-ca-central-1.env
├── setup.py
└── utils                       ###### This is a correct location.
    ├── __init__.py
    ├── standalone_output.py
    ├── version.py
    └── version_check.py