0

I'm getting a bit confused by how import works in Python (3.5). I thought that using something like "import x" would be just as writing whatever is written into "x.py", however it does not seem like it.

I have the following structure:

  • main.py
  • Package1
    • module1.py

Now, just for completion, the module is:

## module1.py
import numpy as np
import matplotlib.pyplot as plt

def plot(x,y):
    A = plt.figure()
    plt.plot(x,y)
    plt.show()
    return A

So it really doesn't do anything that matplotlib.pyplot would not do. Now my main.py is just calling it like this:

## main.py
from Package1.module1 import plot
a= plot([1,2,3],[2,4,6])

And this works. So I'm assuming that it actually imports matplotlib.pyplot as plt, since otherwise the function plot would not work. However, if now I add anything to main.py such as plt.figure() or np.array(), it says that it does not recognize these.

So the actual question is... Have I imported matplotlib.pyplot to the main namespace? If not, is there a different namespace here? If there is, if I now wanted to use matplotlib in the main.py below that code, and I imported it again with import matplotlib.pyplot as plt, would I be importing it twice?

I am a bit lost in the hierarchy here.

Daniel V.
  • 67
  • 1
  • 4
  • 1
    Possible duplicate of [Use 'import module' or 'from module import'?](https://stackoverflow.com/questions/710551/use-import-module-or-from-module-import) – buran May 17 '19 at 11:24

1 Answers1

2
from Package1.module1 import plot

When you do this, only the name plot from the module Package1.module1 is pulled into your current module (main.py). The other symbols that may be defined in Package1.module1 are not pulled in.

But.

The whole module in Pacakge1.module1 is run. If you put a print('something') in the global part of the module like:

## module1.py
import numpy as np
import matplotlib.pyplot as plt

print('something')

def plot(x,y):
    A = plt.figure()
    plt.plot(x,y)
    plt.show()
    return A

It will be printed.

something

So your function definition is indeed loaded into the runtime. Hence your function works. But you only have access to the function that you imported i.e. plot and not anything else.

rdas
  • 20,604
  • 6
  • 33
  • 46
  • Oh. I see. However, If I import "all" using `import Package1.module1` in `main.py`, it still does not recognize `plt` nor `np`. And then the imports would be run twice in main if I add them, right? – Daniel V. May 17 '19 at 11:31
  • Python will cache imports. So not exactly. Those t hings will be imported only once. – rdas May 17 '19 at 11:33
  • I see. Just one last thing. If I use `import Package1.module1 as alias`, I can then use `alias.plt.figure()` and it works. However, if I use `import Package1.module1`, without an alias, it does not let me use `Package1.module1.plt.figure()`. Is there another way to call the inner imports? – Daniel V. May 17 '19 at 11:36
  • Using `__all__` https://stackoverflow.com/questions/44834/can-someone-explain-all-in-python – rdas May 17 '19 at 11:38
  • I see. Thank you very much, kind sir/madam :) – Daniel V. May 17 '19 at 11:40