-1

I'm having some problems with this:

I have two files: random and random2

random has this:

    import random2
    print(random2.verb_list)
    print(random2.x)

random2 has this:

    verb_list = ['x', 'y', 'z']
    other_list = ['1', '2', '3']
    something_else = False

    def Main():
        global x
        x = 1
        print(verb_list)
    if __name__ == "__main__":
        Main()

It gives me this error when I run random:

AttributeError: module 'random2' has no attribute 'x'

Is there a way so I can call the variable x in random? I have python3

6
  • 1
    Try actually declaring x outside your function Commented Aug 3, 2016 at 14:35
  • Possible duplicate of python using variables from another file Commented Aug 3, 2016 at 14:37
  • @WayneWerner I've tried that and it works. But what I want to do is change the x value in a function. Is it possible? Commented Aug 3, 2016 at 14:41
  • @Two-BitAlchemist Yup, I searched that and used part of the code, but still, it doesn't help. Commented Aug 3, 2016 at 14:44
  • @luistripa You've got to provide an actual error description if you want help. "I looked at the right way to do it that works for everyone else but it doesn't help me." doesn't do anything for anyone involved. What are you trying to do, what exactly have you tried in order to accomplish it, and what errors, if any, are you getting? Commented Aug 3, 2016 at 14:48

1 Answer 1

2

The variable x is not created until the Main() function is run. Importing a module means that __name__ isn't set to __main__, so the function is never executed.

You must execute the Main() function. Put random2.Main() in the file random.py after the import line.

The name random is a very poor name for a module, since it collides with the standard python random module. It can create unexpected side effects.

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

1 Comment

Sorry about the file name...it was the first thing that came to my head. Anyway, it worked!

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.