首页 > 解决方案 > Access to global variables when importing a custom class

问题描述

I have the following structure in my project:

    myProject
    |- classes
       |- myclass.py
    |- main.py

But myclass.py doesn't see variables defined at main.py

main.py:

import pandas as pd
from classes.myclass import myclass
outputPath = '/some/path/'
classInst = myClass()

myclass.py:

import pandas as pd

class myClass:
    def __init__(self):
        global outputPath
        self.outputPath = outputPath
        dataFrame = pd.DataFrame(columns=['col1', 'col2'])

Is it possible to define global variables in outputPath in main.py only and use in that in all classes?

标签: python

解决方案


推荐阅读