1

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.

Hasan
  • 247
  • 7
  • 22

1 Answers1

0

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
frankfalse
  • 1,553
  • 1
  • 4
  • 17
  • If I do that, pylint tells me "Unable to import 'utils.print'" – Hasan Jul 13 '23 at 15:17
  • Sorry I don't know `pylint`! Could you try to execute the `__main__.py` script by the `python` interpreter installed in your system? This means by the command `python __main__.py` and from the folder `wpdetect`. – frankfalse Jul 13 '23 at 15:23
  • 1
    Yes it works, from `wpdetect` directory or even any level above. But pylint is still holding the importing as red mark. – Hasan Jul 13 '23 at 15:52
  • Documentation from pylint is not making sense to me either https://pylint.readthedocs.io/en/latest/user_guide/messages/error/import-error.html – Hasan Jul 13 '23 at 15:53
  • OK `pylint` finds some problems and from the documentation (https://pylint.readthedocs.io/en/latest/user_guide/messages/error/import-error.html) suggests to install a package. But if your need is only execute the **local code** and not the package code, I think that you can avoid to use `pylint`. An other solution could be uninstall the package `wpdetect` from the system by `pip uninstall wpdetect`! – frankfalse Jul 13 '23 at 15:58
  • I know it works if I install the package but the problem is during testing. When I write a unit test, and make any changes to the test file, I don't want to install the package everytime. – Hasan Jul 13 '23 at 16:03
  • I mean sometimes I have to tweak the methods that I am testing and in those cases, I would have to reinstall. – Hasan Jul 13 '23 at 16:06
  • I'm sorry but I can't understand your goal. Your question ask **How can I import and use the function print_logo() from the local file print.py instead from the installed module?**, so the answer suggests you a method to do that, but may be you have to ask some other thing which include the mandatory use of `pylint`; but this is an other question. – frankfalse Jul 13 '23 at 16:09
  • Yeah, you are right. I should not mix another concern with this question. – Hasan Jul 13 '23 at 16:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/254482/discussion-between-frankfalse-and-hasan). – frankfalse Jul 13 '23 at 16:13