1

I'm using Visual Studio Code. I had a python script that was working, but realized that I'd like to make it a class for other scripts to use. The files were structured as follows:

pha
   libclass.py
   __init.py__
myScript.py

And it worked fine. myScript.py imported pha.libclass without a problem. Wanting to add myScript.py to my library, I moved it into the pha folder

pha
    libclass.py
    __init.py__
    myScript.py

And when I try to run it now, the from pha.libclass import * call causes an error:

Exception has occurred: ModuleNotFoundError
No Module named 'pha'

Am I missing something?

martineau
  • 119,623
  • 25
  • 170
  • 301
beezle
  • 35
  • 8
  • Have you tried restarting VSC? – Pari Baker Dec 12 '18 at 23:54
  • 1
    To make `pha` a package it needs a file named `__init__.py` – Peter Wood Dec 12 '18 at 23:56
  • @PariBaker I have. No luck. – beezle Dec 13 '18 at 00:05
  • @PeterWood It has one. – beezle Dec 13 '18 at 00:06
  • @beezle not if it's called `__init.py__` it doesn't. – Peter Wood Dec 13 '18 at 00:27
  • @PeterWood You don't need (empty) `__init__.py` files to define a package in Python 3, which this question is tagged as. See [this](https://stackoverflow.com/a/37140173/425458). Though the point that `__init.py__` is the wrong name is absolutely correct. – tel Dec 13 '18 at 01:06
  • When you moved `myScript.py` into the `pha` directory, you made it part of the package, but by running it directly, you're attempting to do so as though it wasn't. You can get the `import` to work in that case by using just a regular `from libclass import *` since `libclass.py` is now in the same directory as `myScript.py`. – martineau Dec 13 '18 at 01:34
  • There's good information about this kind of thing in the accepted answer to the question [Relative imports for the billionth time](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time). – martineau Dec 13 '18 at 01:36

1 Answers1

1

python cant find the module pha, you need to change your import statement to import libclass / from libclass import * in myScript.py, cause both scripts are now side by side under the same directory

JoshuaCS
  • 2,524
  • 1
  • 13
  • 16
  • Right... so the problem is, I've oversimplified the issue. I have a script in `pha.ps.sbpro` (three nested directories) that `libclass` imports. Once I make the change you suggest, those imports break. I'd rather not start changing any of those imports, because there's more scripts at the root level that use this whole library. Am I not supposed to be running scripts in a package structure at all different levels? – beezle Dec 13 '18 at 00:29
  • 2
    @beezle: No, you're not. – martineau Dec 13 '18 at 00:52
  • @beezle Why is my answer down voted? I think it fixes the problem YOU POSTED HERE (sorry if you oversimplified it). Try to correct your question, please. – JoshuaCS Dec 13 '18 at 01:01
  • @beezle: The change I suggested was just in **myScript.py**. Can you just post all the code and files/directories you are working on? – JoshuaCS Dec 13 '18 at 01:17
  • @beezle will you update your question and remove the oversimplification any time soon? – JoshuaCS Dec 13 '18 at 20:38
  • @JosuéCortina unfortunately, the full project is pretty large, and since it's my employer's property, it's not mine to divulge. I ended up circumventing the problem by divorcing the call to execute the script from the file the class was in, and creating a script at the root level to call it, like I treat the rest of my scripts. Thanks. – beezle Dec 13 '18 at 22:17