简体   繁体   English

导入自定义包和模块时遇到问题?

[英]Problems in importing self-defined packages and modules?

I am using Spyder. 我正在使用Spyder。

I have just started building a project. 我刚刚开始建立一个项目。 Its architecture is currently as shown. 当前其架构如图所示。

在此处输入图片说明

As shown, I have 2 packages, one of which has a module called trajectorygeneration now. 如图所示,我有2个软件包,其中一个包含一个称为trajectorygeneration的模块。 Both the __init__.py files are automatically generated. 这两个__init__.py文件都是自动生成的。 I am trying to import the module into the main.py , but I ended up with such error messages: 我试图将模块导入到main.py ,但最终出现这样的错误消息:

>>> from generation import trajectorygeneration
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named generation

How may I fix this problem? 我该如何解决这个问题?

( Spyder dev here ) This is a bug in Spyder. 此处Spyder开发人员 )这是Spyder中的错误。 We are not adding the project's path to PYTHONPATH and that's why you can't import your project in the console after you open it. 我们没有将项目的路径添加到PYTHONPATH,这就是为什么打开项目后无法在控制台中导入项目。 I created an issue for you and we'll fix it in our next major release (ie 2.3). 我为您创建了一个问题 ,我们将在下一个主要版本(即2.3)中予以解决。

A workaround for now (as it was mentioned by @Jblasco) is to use our PYTHONPATH manager to add the project's path by yourself. 目前的解决方法(如@Jblasco所述)是使用我们的PYTHONPATH管理器自行添加项目的路径。 After doing it, you need to open a new console so that changes can take effect. 完成此操作后,您需要打开一个新控制台,以使更改生效。

Might it be that generation is not in your path and python can not find the package? 可能是因为生成不在您的路径中,而python无法找到包?

See: Expand Python Search Path to Other Source 请参阅: 将Python搜索路径扩展到其他源

Also, Spyder has a "pythonpath" manager (at least mine has) where you can include any folder in it. 另外,Spyder有一个“ pythonpath”管理器(至少我的有),您可以在其中包含任何文件夹。 Problem is, that if you do it this way, then the python console, for example, will most likely not include those, so you could not import in the console. 问题是,如果以这种方式进行操作,则例如python控制台很可能将不包含这些内容,因此无法导入控制台。

It seems is caused by typo. 看来是由错字引起的。

In your picture, target file name is trajectorygeneration.py . 在您的图片中,目标文件名为trajectorygeneration.py But main.py would import module named trajectorygeneration's' . 但是main.py将导入名为trajectorygeneration's'模块。

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

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