I want to import the common module

Asked 1 years ago, Updated 1 years ago, 415 views

I'm studying a book called "Deep Learning from scratch."
If you continue to read, you will be importing a module called common, where the following error occurs:

ModuleNotFoundError: No module named 'common'

I tried to install a common module, but it didn't seem to be open to the public in the first place, so I couldn't install it.
Also, github had a file of O'Reilly's official common module, but can I use this file as a module?If possible, please let me know how to do it.

ラーニングDeep learning source code from scratch <
https://github.com/oreilly-japan/deep-learning-from-scratch

python python3 deep-learning

2022-09-30 22:03

1 Answers

There is a folder called common in the repository to which you are referencing it, and it is now import.

For example, the second to fifth lines of ch06/weight_init_compare.py contain the following lines, which are the preparations for common to be import.

importos
import sys

sys.path.append(os.pardir)#Configuring to Import Files in the Parent Directory

The above code assumes that the folder configuration in the repository is expanded and used as it is.

If you want to do something new, create your own folder (e.g., sample01) in the same hierarchy where ch01 is located in the folder where you expanded the repository above, and insert the above code into import before

If you don't want to keep such a folder configuration the same, you can do the same by specifying the os.pardir portion of the code above as the absolute path to the parent folder of the expanded common.

See below for specifications.
6.1.2.Module search path
6.2.Standard Module

In a development environment where the Jupiter-lab/notebook working folder may be separate from the folder where the script/notebook files reside, the above method may not work.
Because os.pardir is the string constant '..', it indicates the parent folder of the current working folder.
os.pardir

String constant used by the operating system to reference the parent directory. For POSIX and Windows, it is '..' and is also available from os.path

There was an article in the resources of the reference repository.
sys.path.append(os.pardir) didn't work, so I specified the full path to os.pardir and it worked #25
The questioner's environment is macos, but it looks like a console, so it's just for reference.

For example, if you can get the path of the script file itself, you can get the parent folder from it.
Get the path of the script running on Python:__file__, os.path.abspath, os.path.dirname, os.path.basename
Get the absolute path of the parent directory in Python

However, it seems that there is no standard way to get the path of the notebook file using Jupiter-lab/notebook.
But here's an article I found that I made my own package for it, so why not try it?
[Jupyter] Created a package where running ipynb files can get their own paths

Or there is an article like this, but if you want to divide your notebook into various folders, it may not be suitable.
How to change the home directory of JupiterLab


2022-09-30 22:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.