2

Assume I have the following file structure:

  Package/
           __init__.py
           A.py
  B.py

Inside __init__.py I have the following:

__init__.py/
 import numpy as np
 import pandas as pd

Then I issue the following in the A.py script:

A.py/
from Package import *

However, I receive an error message that no module name Package is defined.

ModuleNotFoundError: No module named Package

I thought from Package import * means running everything in the __init__.py.

I can run the A.py content and use the init import from B.py as I expected.(using from Package import *)

I am using VSCode and Anaconda and my OS is Windows 10.

I can append the project folder every time to my PythonPath using the following commands:

sys.path.append("Path to the Package")

But I do not want to run this piece of code every time.

Can anyone explain what is the problem? Is this a new problem in Python since I do not recall having such issues in the past?

math
  • 341
  • 4
  • 14
  • 1
    `A.py` is supposedly inside `Package` (i.e. part of the package), then inside it you try to star import everything from `Package`. This is not going to work and even if it did it will cause circular import. Not to mention that star imports are bad. – buran Jun 30 '21 at 05:03
  • OK. The circular package might be an issue but this is not the case here. However why my command is not working. How can I use what I imported have inside __init__.py then? This should be a new thing since this was not a problem while ago. – math Jun 30 '21 at 05:10
  • 2
    Check your `sys.path`. Python is searching the `Package` at places listed there. https://docs.python.org/3/library/sys.html#sys.path – VPfB Jun 30 '21 at 05:26
  • Does this answer your question? [Relative imports for the billionth time](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time) – Davis Herring Jun 30 '21 at 14:27

2 Answers2

1

Because if you run the B.py, the Parent folder of Package folder will be added into the sys.path, be equal to you add sys.path.append("Path to the Package") in the A.py file.

But when you run the A.py, it will add the Package folder instead of the Parent folder of Package folder to the sys.path.

sys.path:

A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.

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.

If you are running the python file in debug mode(F5), and the Package folder was the subfolder of your workspace, you can configure the PYTHONPATH in the launch.json file:

  "env": {
    "PYTHONPATH": "${workspaceFolder}"
  },
Steven-MSFT
  • 7,438
  • 1
  • 5
  • 13
0

In your A.py script use just use import file.py and do not use the star. Or, put all your files in a second Package2 directory and use from Package2 import * from your current A.py file.

bauderr
  • 47
  • 10