0

I want to pass a variable from fileA.py to fileB.py but i don't know how use (import) it in fileB.py.

I have this situation (frames folder and file.A are at same level)

frames
  |
  folder
    |
    fileB.py

fileA.py

In the fileA.pyi have an image and i want to pass it in fileB.py

immagine= cv2.imread('image.jpg')
os.chdir("frames/folder")
subprocess.call(["python", "fileB.py", 'immagine'])

I think this works well but i don't know how import immagine in fileB.py. Maybe i should to use:

from ..fileA.py from immagine

but not works and i have this error: ImportError: attempted relative import with no known parent package

I hope you can help me... i'm really new in python (like Ide i use pycharm and i use it to install modules - of course fileB.py and fileA.py aren't modules but normal python files)

4
  • 1
    Is there a reason you're using a subprocess? (Other than "I don't know how to import a package"?) Commented Jul 29, 2022 at 14:05
  • I think OP wants to pass image data as a parameter not just the string 'immagine'. I think it is a question about shared memory Commented Jul 29, 2022 at 16:46
  • Yes i want to pass image data. Really should be a for loop and for each image (in a folder) i want to run fileB.py and pass image of iteration Commented Jul 29, 2022 at 17:07
  • don't even think about shared memory, or subprocesses. all you need is to figure out how python imports work. then you can just pass the numpy arrays to whatever function is in your other module. Commented Jul 29, 2022 at 18:03

2 Answers 2

1

I can think of two solutions, one using your subprocess call and another one with import:

Easiest solution

You can treat them as console command parameters and read them with sys.argv.

For example in fileB.py:

from sys import argv
var = argv[1]

print(var)

Should output: immagine

Arguably best solution

Another way of doing it is to make both fileA.py and fileB.py part of the same module, even if fileB.py is a different submodule. For example:

mymodule
|    |  |
|    | fileA.py
|    |
|   folder
|      |
|     fileB.py
|  
__init__.py (this marks mymodule as a moduke)

And then in fileB.py:

from mymodule.fileA import immagine

Take into account that you must run fileA with python -m mymodule.fileA for this to work.

Sign up to request clarification or add additional context in comments.

Comments

0

Your requirements are unclear, so I'll just question all of them. You should not use a subprocess and you should not use shared memory, or any other hack.

You should simply define a function in fileB.py:

# frames/folder/fileB.py

def do_something(im):
    # do something to im
    # return something
    return im.shape # for example

if __name__ == '__main__':
    # when running this script directly, do whatever you need to here
    # e.g. use a command line argument, read as an image file, call your function...
    ...

and then, in fileA.py, you do this:

# fileA.py

from frames.folder.fileB import do_something
res = do_something(image)
print(res)

And that's all!

If you need the opposite, i.e. in the inner file, you want to call functions defined in the outer file, then you do need to make this a package, so you can use a relative import.

2 Comments

i want to pass image (immagine) from fileA.py to fileB.py and in fileA.py i need to call fileB.py. I tried your code but now work :(
do you understand that you can't (should not) call entire files, but you can call functions in those files? "it didn't work" is not debuggable. at least tell me what happened.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.