PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 24, 2022

[FIXED] How do I run the main script of a Python package directly?

 August 24, 2022     module, package, python, python-3.x     No comments   

Issue

I have an issue on a large set of scripts I've put into a package, and setup a test repo package_test to get things working, as shown below. I'm using Python 3.7.4 on Windows 10, with VS Code as my IDE.

package_test/
-- package_test/
---- __init__.py
---- __main__.py
---- package_test.py
---- module_1.py
-- setup.py

I have gotten things to work so that I can run this as a module, using python -m package_test from the root of this directory. However, if I try to run the package_test.py module directly (such as having VS Code launch it, or to use the debugger), I get an error.

The problem appears to be with imports. Why can't I run the package_test.py script directly?


Here are the relevant files:

__init__.py

from .module1 import *

__main__.py

import package_test.package_test

def main():
    package_test.package_test.main()

if __name__ == '__main__':
    main()

package_test.py

import package_test
from package_test.module1 import *

def main():
    package_test.module1.main()

if __name__ == '__main__':
    main()

module1.py

import package_test
from .module1 import *

def textfx():
    print('Hello textfx!!')

def main():
    package_test.module1.textfx()

if __name__ == '__main__':
    main()

The error when running directly is:

USER@PC MINGW64 /c/Code/python/package_test (master)
$ C:/apps/Python37/python.exe c:/Code/python/package_test/package_test/package_test.py
Traceback (most recent call last):
  File "c:/Code/python/package_test/package_test/package_test.py", line 1, in <module>
    import package_test
  File "c:\Code\python\package_test\package_test\package_test.py", line 2, in <module>
    from package_test.module1 import *
ModuleNotFoundError: No module named 'package_test.module1'; 'package_test' is not a package

But, when I run this as a module, the result is:

USER@PC MINGW64 /c/Code/python/package_test (master)
$ py -m package_test
Hello textfx!!

Solution

As can be seen from the documentation of sys.path:

As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. [...]

Since you are running package_test$ python package_test/package_test.py the first place where Python will look for modules in your example is package_test/package_test. Here it finds the module package_test.py which you import via import package_test. Now this module is cached in sys.modules. When you do from package_test.module1 import * it fetches package_test from the module cache and reports back that this isn't a package and thus it can't perform the import.

You should rename that package_test.py script to something else. Why does it exist in the first place when all it does is importing from another module and __main__ just imports from that script. Why can't you run __main__.py and have it import from module1 directly?

You can place this code at the top of package_test.py and inspect the output:

import sys
print(sys.path)

import package_test
print(sys.modules)
print(package_test)


Answered By - a_guest
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing