简体   繁体   English

Python3 - ImportError:没有命名的模块

[英]Python3 - ImportError: No module named

I have 2 folders:我有 2 个文件夹:

my_python
     code.py

MyCode
     TestEntry.py

When I run the following commands:当我运行以下命令时:

cd /data/my_python
python3 code.py

The above works.以上工作。

However, if I in my home folder and then run this:但是,如果我在我的主文件夹中然后运行这个:

python3 /data/my_python/code.py

I get the following error:我收到以下错误:

Traceback (most recent call last):
  File "/data/my_python/code.py", line 4, in <module>
    from TestEntry import TestEntry
ImportError: No module named 'TestEntry'

Here is the code:这是代码:

import sys
import os
sys.path.append(os.path.abspath('../MyCode'))
from TestEntry import TestEntry
TestEntry().start(507,"My Param1","/param2",'.xyz',509)

Can you help me how to fix this?你能帮我解决这个问题吗?

That happens because, as @mkrieger1 mentioned, your sys.path gets messed up.发生这种情况是因为,正如@mkrieger1 所提到的,您的sys.path搞砸了。 I have a previous answer here which explains how to set it.在这里有一个先前的答案,它解释了如何设置它。 By sys.path getting messed up, I mean that python will look in the dir that you are running from, not the dir that the script you are running is in. Here is the recommended method: sys.path搞砸了,我的意思是 python 将查看您正在运行的目录,而不是您正在运行的脚本所在的目录。这是推荐的方法:

import sys, os
sys.path.append(os.path.abspath(os.path.join('..', 'MyCode')))
... (your code)

or或者

import sys, os
sys.path.append(os.path.abspath(os.path.join(__file__, '..', 'MyCode')))
... (your code)

This way python will look in the dir of the file you are running as well.这样 python 也会在您正在运行的文件的目录中查找。

You are adding a relative path to sys with your line sys.path.append(os.path.abspath('../MyCode')) .您正在使用sys.path.append(os.path.abspath('../MyCode'))行添加到sys的相对路径。 Instead, you need to import relative to that file you are calling.相反,您需要相对于您正在调用的文件进行导入。 Try this:尝试这个:

import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from TestEntry import TestEntry
TestEntry().start(507, "My Param1", "/param2", '.xyz', 509)

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

相关问题 导入错误:没有名为 _winreg python3 的模块 - importError: no module named _winreg python3 heroku python3:ImportError:没有名为“ encodings”的模块 - heroku python3: ImportError: No module named 'encodings' Python3 ImportError:在Ubuntu上没有名为&#39;_tkinter&#39;的模块 - Python3 ImportError: No module named '_tkinter' on Ubuntu python3-ImportError:没有名为twython的模块 - python3 - ImportError: No module named twython 导入错误:没有名为“cv2”的模块 Python3 - ImportError: No module named 'cv2' Python3 Raspbian ImportError 上的 Python 和 Python3 配置无模块命名错误 - Python and Python3 Configuration on Raspbian ImportError No Module Named Error ImportError:没有带有virtualenv python3的名为“ bs4”的模块 - ImportError: No module named 'bs4' with virtualenv python3 我从 python3 得到 ImportError no module named pytube - I am getting ImportError no module named pytube from python3 Python3错误“ImportError:没有名为的模块”有__init__.py - Python3 error “ImportError: No module named” have __init__.py Python3 ImportError:没有名为“ google.protobuf”的模块 - Python3 ImportError: No module named 'google.protobuf
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM