Import by directly specifying the directory path

Note that the test did not pass with Import Error in the existing project

When the project structure is as follows

root/
 ├ module/
 │ └ submodule/
 │  └ to_import_module
 └ tests/
  └ test1.py

Trying to import to_import_module in test1.py

import module.submoule.to_import_module


 Even as

#### **` No module named to_import_module`**
```importerror

 Will be pushed back. However, for this reason, I specified a path
 It's also a hassle to change project settings and so on.
 So here I use sys and os modules

import os import sys sys.path.append(os.path.dirname(os.path.abspath(file)) + '/../module/submodule') import to_import_module

 Import becomes possible by expanding the exploration path range.
 It is good to specify a relative path so that it works in other environments.

 However, this method makes me angry with "pep8 E402",
 ** Test execution is heavier than code rules **
 So please forgive me.
 Of course, it is NG to use for the favorite code


Recommended Posts

Import by directly specifying the directory path
BeautifulSoup trick: Decide the Tag by specifying the path
Install by specifying the version with pip
Read the file by specifying the character code.
List all files under the current directory line by line with full path
Upload the image downloaded by requests directly to S3
[Reintroduction to python] How to import via the parent directory
Python3 datetime is faster just by specifying the timezone
File renaming using the full path received by the shell
Access Github by specifying the SSH key in GitPython
Linux (about directory path)
[Note] Import of a file in the parent directory in Python
[Python3] Call by dynamically specifying the keyword argument of the function
Hook to the first import of the module and print the module path
[Python] Get the script execution directory with an absolute path