Reputation: 165
Suppose a certain directory checkerbot (You can download the files). From my current directory, I have this directory called checkerbot
which contains 4 files, i.e. base.py
, checkers.py
, __init__.py
and model.py
.
In [1]: import checkerbot.checkers as ck
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-d7411e2bfdb5> in <module>
----> 1 import checkerbot.checkers as ck
~/../main/checkerbot/checkers.py in <module>
----> 1 import base
2
3 class Game:
4 # Initializes the class.
5 def __init__(self):
ModuleNotFoundError: No module named 'base'
This error told me I have no base.py
in thecheckerbot
directory, but it is false. How can I fix that? For your information, I am using python 3.7.6.
UPDATE
In [2]: import .base
File "<ipython-input-2-e4256d58e84b>", line 1
import .base
^
SyntaxError: invalid syntax
Suppose my current directory is main
and checkerbot
is in main/
. If I run ipython
and then from . import base
then I got :
In [1]: from . import base
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-a8bda1f23f19> in <module>
----> 1 from . import base
ImportError: attempted relative import with no known parent package
Another error :
In [5]: from checkerbot import base
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-5-3203dc774184> in <module>
----> 1 from checkerbot import base
~/../main/checkerbot/base.py in <module>
1 import tensorflow as tf
2 import numpy as np
----> 3 from model import model
4
5 agent = tf.keras.models.Sequential()
ModuleNotFoundError: No module named 'model'
Upvotes: 2
Views: 4615
Reputation: 3
justwrite the name of the folder contain in it
ex:
from pymouse.base import PyMouseMeta, PyMouseEventMeta
Upvotes: 0
Reputation: 12201
Use
from . import base
That will provide a relative import of the base
module in the checkerbot
package.
A similar thing holds for the model
module:
from .model import model
Upvotes: 1
Reputation: 423
Use this instead
from . import base
Edit
Consider the following example:
└── foo
├── __init__.py
├── module_bar.py
└── module_foo.py
$ cat foo/module_bar.py
from . import module_foo
print(module_foo.load())
$ cat foo/module_foo.py
def load():
print("module foo is loaded")
$ python
Python 3.8.2 (default, May 4 2020, 20:05:11)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import foo.module_bar
module foo is loaded
This means, whenever you're importing a module from directory, your imports within the directory should start with .
Upvotes: 1
Reputation: 127
Are you running the script from the checkbot directory? If are running the script from the parent directory just try out from . import base
or from checkerbot import base
.
Currently python probably searchs for base.py
in your parent directory.
Upvotes: 1