ImportError: No module named… (basics?) [closed]

依然范特西╮ 提交于 2019-12-13 08:07:30

问题


A very basic question that has been confusing me. Can't seem to find any solutions online so far...

I have a simple script and want to import another script I just made in the same directory.

What is the proper way of doing this?

I just tried combos of import myfile, from myfolder import myfile, import myfolder.myfile, etc

I get ImportError: No module named 'myfile'

Cheers


回答1:


This is because your current directory isn't on the PYTHON_PATH, which is what import searches when you call it. See the documentation.

If you want an immediate fix, you can use the following:

import sys, os
sys.path.append(os.path.abspath(os.path.dirname(__file__)))

This adds the directory containing the script's file to the python path.

This will only work if the directory these scripts are in is set up as a package.



来源:https://stackoverflow.com/questions/22275056/importerror-no-module-named-basics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!