0

I'm creating my first package and I can not figure out how to create a simple test.py file to debug my code.

Consider the following file structure.

|--MyPackage
  |--test.py
  |--PackageName
      |--__init__.py
      |--module.py

Inside test.py I have: from .src.module import SomeClass

And of course this gives me the dreaded "attempted relative import with no known parent package" message.

I know I could just install the package and then import it with from PackageName.module import SomeClass but then I'm using the code installed on my system and not the code that I am actively editing.

There has got to be some kind of standard way for testing and debugging a package right? Despite all the searching I've done, I can't seem to find any kind of solution.

I'm running test.py with python3 test.py

If it's helpful, here is a screenshot of my actual project folder structure:enter image description here

Dallin Davis
  • 421
  • 7
  • 18
  • Are you running `test.py` as a standalone Python script (`python3 test.py`), or as a submodule of a package (`python3 -m MyPackage.test`)? It looks like you want to do the latter. Relative imports don't make sense with the former. – Brian61354270 Jun 04 '21 at 19:48
  • I'm using `python3 test.py` – Dallin Davis Jun 04 '21 at 19:51
  • 1
    Is `MyPackage` supposed to be a package? Also re: "but then I'm using the code installed on my system and not the code that I am actively editing", you may be looking for [When would the -e, --editable option be useful with pip install?](https://stackoverflow.com/questions/35064426/when-would-the-e-editable-option-be-useful-with-pip-install) – Brian61354270 Jun 04 '21 at 19:52
  • The -e option is exactly what I needed. Thanks so much. I would still like to better understand python imports and why my implementation doesn't work. Know of any resources that can explain this to me simply? – Dallin Davis Jun 04 '21 at 20:11
  • 3
    If you haven't read it already, the official documentation is the first place to go. The sections on [Modules & Packages](https://docs.python.org/3/tutorial/modules.html) (especially sections 6.1.2 and 6.4) and [the import system](https://docs.python.org/3/reference/import.html) cover just about everything there is to know. These SO questions are also good reads: [Importing files from different folder](/questions/4383571/), [Relative imports for the billionth time](/questions/14132789/) (and links therein), [Difference between a Python module and a Python package?](/questions/7948494/). – Brian61354270 Jun 04 '21 at 20:17
  • The following three references give excellent advice on debugging your code. [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/), [Six Debugging Techniques for Python Programmers](https://medium.com/techtofreedom/six-debugging-techniques-for-python-programmers-cb25a4baaf4b) or [Ultimate Guide to Python Debugging](https://towardsdatascience.com/ultimate-guide-to-python-debugging-854dea731e1b) – itprorh66 Jun 04 '21 at 20:49

1 Answers1

0

Besides the literature on the import structure for python scripts, I managed to find another way using PyCharm.

  1. Install the python package with pip install <package>
  2. Navigate to the package and open the folder with PyCharm
  3. On the top right corner open the dropdown and select Edit Configurations
  4. Choose the '+' icon and select python
  5. In the Configuration tab click the arrow next to Script path: and select Module name and enter the module name next to it

You're now running the package at it's entry point and can use the python debug functionality.

extraj
  • 41
  • 3