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

LightCC

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!!
a_guest

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I directly run the resource creation script with Pycharm before launching the main python script of a Qt5 app?

How do I run an npm script directly in VS code terminal

How do I properly run a separate python script from main python file?

How do I install and run a python script

How do I run the ./configure script on git package?

How do I run functions inside my main function in python?

How do I use tensorflow 1.14.0 in a python package and run it in Dockerfile?

How do I create a deb package for a single python script?

How do I access functions in the main package?

How do I run a python script in parallel with different arguments in linux?

How do I run custom python script in Google App engine

How do I run a python script at specific time in a day automatically?

How do I run a python script and pass arguments to it in C

How do I run a local Python script on a remote Spark cluster?

How do I run a Python script on my web server?

How do I run a Python script on my web server?

How do I run another script in Python without waiting for it to finish?

How do I run a Python script from C#?

How do I run a Python script from C#?

How do I run a Python script in the background and restart it after a crash?

How do I run a python script whenever a file is created in a directory?

How do I run a python script using an already running blender?

How do i run python script on all files in a directory?

How do I schedule a Python script to run in a Linux VM?

How do I run a python script on boot on Google Coral?

How do I run a PowerShell script with parameters from Python

How/where do I upload a (python) script to jenkins to run?

How do I connect a Python script to run combinations on a webpage input?

How do I run a function from another script in Python