Home > Enterprise >  How to import variables stored in a python file in a different directory?
How to import variables stored in a python file in a different directory?

Time:05-05

My folder structure looks like the following:

Folder structure looks like this

Backend/config.py hostName = "0.0.0.0"

Backend/Utils/upload.py #I want to use the hostName in this file, how to import it?

I tried using the following in the upload file:

import os, sys
sys.path.append(os.getcwd())
import config as appConf

If I execute my program from the folder Backend:

\Backend> py .\utils\upload.py

it is working fine. But if I keep it as

\Backend\Utils> py upload.py

it is not working. I would like to understand how this works. I am trying to execute this from my python virtual environment.

Kindly help me out.

I am sorry if my question is not clear or very simple.

CodePudding user response:

You should declare your varriable in

class

then import your upload.py file into the desired file and get. e.g.

Upload.hostName

CodePudding user response:

\Backend> py .\utils\upload.py - Here, there is a 'utils' folder. And you said it is working fine.

\Backend\Utils> py upload.py - Here, it is 'Utils'. Could you please clarify on this ?

  • Related