简体   繁体   English

Python 脚本在 PyCharm 中有效,但在终端中无效

[英]Python script works in PyCharm but not in terminal

I'm currently trying to import one of my modules from a different folder.我目前正在尝试从不同的文件夹导入我的模块之一。 Like this...像这样...

from Assets.resources.libs.pout import Printer, ForeColor, BackColor

This import method works completely fine in PyCharm, however, when i try to launch the file in cmd or IDLE, i get this error.此导入方法在 PyCharm 中完全正常工作,但是,当我尝试在 cmd 或 IDLE 中启动文件时,出现此错误。

ModuleNotFoundError: No module named 'Assets'

This is my file structure from main.py to pout.py:这是我从 main.py 到 pout.py 的文件结构:

- Assets
  - main.py
  - resources
    - libs
      - pout.py

Any clue about how i could fix this?关于我如何解决这个问题的任何线索? Any help is appreciated !任何帮助表示赞赏!

Edit: The original answer was based on the assumption that the script you're running is within the folder structure given, which a re-read tells me may not be true.编辑:最初的答案是基于您正在运行的脚本在给定的文件夹结构内的假设,重新阅读告诉我可能不是真的。 The general solution is to do一般的解决方案是做

sys.path.append('path_to_Assets')

but read below for more detail.但请阅读下文了解更多详情。

Original answer原始答案

The paths that modules can be loaded from will differ between the two methods of running the script.可以加载模块的路径在两种运行脚本的方法之间会有所不同。

If you add如果你添加

import sys
print(sys.path)

to the top of your script before the imports you should be able to see the difference.在导入之前到脚本顶部,您应该能够看到差异。

When you run it yourself the first entry will be the location of the script itself, followed by various system/environment paths.当您自己运行它时,第一个条目将是脚本本身的位置,然后是各种系统/环境路径。 When you run it in PyCharm you will see the same first entry, followed by an entry for the top level of the project.当您在 PyCharm 中运行它时,您将看到相同的第一个条目,然后是项目顶层的条目。 This is how it finds the modules when run from PyCharm.这是从 PyCharm 运行时找到模块的方式。 This behaviour is controlled by the "Add content roots to PYTHONPATH" option in the run configuration.此行为由运行配置中的“将内容根添加到 PYTHONPATH”选项控制。

Adding this path programmatically in a way that will work in all situations isn't trivial because, unlike PyCharm, your script doesn't have a concept of where the top level should be.以一种适用于所有情况的方式以编程方式添加此路径并非易事,因为与 PyCharm 不同,您的脚本不知道顶层应该在哪里。 BUT if you know you'll be running the script from the top level, ie your working directory will be the folder containing Assets and you're running something like python Assets/main.py then you can do但是,如果您知道您将从顶层运行脚本,即您的工作目录将是包含资产的文件夹,并且您正在运行python Assets/main.py类的东西,那么您可以这样做

sys.path.append(os.path.abspath('.'))

and that will add the correct folder to the path.这会将正确的文件夹添加到路径中。

Appending sys path didn't work for me on windows, hence here is the solution that worked for me:在 windows 上附加 sys 路径对我不起作用,因此这是对我有用的解决方案:

Add an empty __init__.py file to each directory

ie in Assets, resources, libs.即在资产、资源、库中。 Then try importing with only the base package names.然后尝试仅使用基本名称 package 导入。 Worked for me!为我工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM