In the file __main__.py
, I am trying to import the function print_logo()
from the module print.py
.
I have noticed that the function print_logo()
is actually being used from the package wpdetect
installed in my system.
How can I import and use the function print_logo()
from the local file print.py
instead from the installed module?
The __main__.py
file,
'''
Entry point for wpdetect.
'''
from wpdetect.utils.print import print_logo
def main():
"""Detects if a website is running WordPress."""
print_logo('1.0.0')
if __name__ == '__main__':
main()
The print.py
file,
"""
Print utility of wpdetect.
"""
def print_logo(version):
"""
Prints the logo with version number.
"""
if len(version) == 0:
raise ValueError(
"Invalid version number. Please provide a valid version.")
print("wpdetect")
print(version)
Directory structure,
- pyproject.toml
- other_files_and_folders
- wpdetect
-- __init__.py
-- __main__.py
-- utils
--- __init__.py
--- print.py
NOTE
Accepted answer clearly addresses the problem mentioned in my original question. However, I did forget to mention the part that I am using pylint
to lint my code.
And when I imported like from utils.print import print_logo
, I was getting an import error warning from pylint
. So, I had to add a configuration of pylint as suggested in this answer.
Since the posted answer clearly solves the question asked, no modification needed there but posting this extra bit so if anyone faces the same issue, you know what you need to do.
I have replicated your Directory structure in my system as followed:
wpdetect
|-- __init__.py
|-- __main__.py
|-- utils
|-- __init__.py
|-- print.py
Note. In my system is not installed any package called wpdetect
.
I have modified only the import in your file __main__.py
as followed (the rest of your code remains without changes):
#from wpdetect.utils.print import print_logo
from utils.print import print_logo
After that I have open a terminal in my Linux system and I have executed the following commands:
# Change directory
> cd /path/to/wpdetect
# Execution of the script `__main__.py`
> python __main__.py
The output of the previous commands is:
wpdetect
1.0.0