4

I have kept the global variables in a file and importing this file into two files. One in which the value of this global variable is being changed and another in which this changed value is to be used.

In 1st file , inside a class

from globals.py import *
.
.
.class ...
    def uploadClick(self):
        global filename
        filename = dialog.askopenfilename()
        print(filename)

In 2nd file

from globals.py import *
.
.
.
  def mainAnalysis():
    global filename , semantic_orientation
    print("filename = "+filename)
    n_docs=0
    with open(filename, 'r') as f:
        count_all = Counter()

In globals file

filename =''

The mainAnalysis function is called after the uploadClickfunction.

I get an error saying the filename is empty when mainAnalysis function runs

1 Answer 1

6

The syntax from globals.py import * makes copies of the variables within globals.py into your local file. To access the variables themselves without making copies, import globals and use the variable directly: globals.filename. You no longer need the global keyword if you access the variable this way.

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

Comments

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.