简体   繁体   English

Importerror:没有在python中命名的模块

[英]Importerror:No module named in python

I created a file named hello.py in /home/Desktop/problems directory and i want to import it to a file present in /home/Desktop/PP 我在/ home / Desktop / problems目录中创建了一个名为hello.py的文件,我想将其导入/ home / Desktop / PP中的文件

#hello.py file
def matrix():
  print "enter into matrix"

and i want to access this hello.py file in a file named new.py so i did like this.. 我想在名为new.py的文件中访问此hello.py文件,所以我喜欢这个..

#new.py file
import sys
import os
sys.path.append(os.path.abspath("/home/Desktop/problems"))
from hello import *
matrix()

But i am getting an error like no module named new 但是我得到的错误就像没有名为new的模块一样

Thanks in advance. 提前致谢。

UPDATE: The directory order was wrong..so it was giving the error.. it should have been /home/valac/Desktop/problems !! 更新:目录顺序错误..所以它给出错误..它应该是/ home / valac /桌面/问题!!

I created both files with the content described here and I ran your new.py: 我使用此处描述的内容创建了两个文件,然后运行了new.py:

python new.py

And it worked just fine. 它运作得很好。 How are you executing or using your new.py script? 你是如何执行或使用new.py脚本的? The problem is probably related to that. 问题可能与此有关。 Can you explain how are you using new.py? 你能解释一下你是如何使用new.py的吗?

Do you have init file in the pp folder? 你有pp文件夹中的init文件吗? You may create init .py under folder pp. 您可以在文件夹pp下创建init .py。

The init .py does not need to contain any code. init .py不需要包含任何代码。 It will change the folder as a module. 它会将文件夹更改为模块。

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

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