0

Say I have some script, with a function my_function. Now, this function needs several packages. So, let's say the file looks like this:

import package_A
import package_B

def my_function():
    do_something

Now, if I want to use this function somewhere else, I may say

from my_file import my_function
my_function()

However, at this point, the call will halt with the error that package_A and package_B are not known.

How do I solve this? Do I have to make all the imports I do for my_function again in the script calling my_function? And if so, is there a way to automatically check and import all the imports in that file?

kfb
  • 6,252
  • 6
  • 40
  • 51
user1991
  • 584
  • 1
  • 4
  • 18
  • 2
    Are the locations of `package_A` and `package_B` visible on your `PYTHONPATH`? – kfb Sep 13 '16 at 13:40
  • This question has already been answered here: http://stackoverflow.com/questions/13598958/import-modules-from-different-folders – JaydenMedia Sep 13 '16 at 13:51
  • @kfb, yes. My apologies if it was unclear, but the packages I'm refering to here are fairly generic ones; think numpy, scipy. So these are on the path. However, if I have imported numpy in my_file, but not in the script where I import my_file, this error appears. That is, how can I make sure when I call my_function, all the relevant packages will be loaded? – user1991 Sep 13 '16 at 14:14
  • @John Hmm. In that case I'm not exactly sure what might be happening. Could you edit your question to give us more of an idea of the layout of your project? I wonder if this is something to do with the absence of an `__init__.py`, though I would expect that to fail to find `my_file` rather than `package_A` or `package_B`. – kfb Sep 13 '16 at 14:21

1 Answers1

-1

You can have several scripts calling each other can can import several packages in each script and it won't throw up an error until you have packages that are required for the functions in that script.

Found this link which will answer your question better

Community
  • 1
  • 1
  • 1
    "You cannot import packages in one script and use that package in another script."... Yes you can – Farhan.K Sep 13 '16 at 13:46