Home > other >  How to import a python script stored in another folder?
How to import a python script stored in another folder?

Time:07-18

I have a file named "generate_tax_report.py" which is stored at location "C:\Users\rites\Desktop\crypto-tax-report\van\adam".

enter image description here

Within file "generate_tax_report.py", I need to import a python script "fifo.py" which is stored at location "C:\Users\rites\Desktop\crypto-tax-report\van\adam\inventory_methods as seen in below image".

enter image description here

I have written:from van.adam.inventory_methods import fifo, but getting error as

File "c:\Users\rites\Desktop\crypto-tax-report\van\adam\generate_tax_report.py", line 13, in <module> from van.adam import transactions as t ModuleNotFoundError: No module named 'van.adam'; 'van' is not a package

Edit: Added structure of van folder: enter image description here

CodePudding user response:

Assuming you have not installed van as a package, the problem is that when you run python generate_tax_report.py from Desktop/crypto-tax-report/van/adam, Python does not add Desktop/crypto-report/van to the path, so from van.adam.inventory_methods import fifo fails because Python does not know about van.

If you moved generate_tax_report.py to Desktop/crypto-tax-report and then ran python generate_tax_report.py from Desktop/crypto-tax-report, it should work.

CodePudding user response:

did you try on this

import sys

sys.path.append('C:\\Users\\rites\\Desktop\\crypto-tax-report\\van\\adam\\inventory_methods')

import fifo

worked on windows machine and print variable value in destination file

this is an example of importing it

CodePudding user response:

I think it can help you
https://www.w3schools.com/python/python_modules.asp or

code:

import subprocess
subprocess.run(["python", "folder/another.py", "5"])

  • Related