Home > Enterprise >  Can't import modules (scripts) in Python
Can't import modules (scripts) in Python

Time:04-01

I'm trying to import my Main.py file to the GUI.py file which is in the same folder. I tried all these ways, but it doesn't work well. I'm using Python 3.10 and PyCharm as IDE.

import Main  # it just runs the Main.py files, doesn't import variables
import Main.py  # same as above
from .Main import *  # error occurred: attempted relative import with no known parent package
from . import Main  # same as above 

I tried creating a package, too (made a new folder and put Main.py and _init_.py in it), but this didn't work either.

Please help, I'm stuck here. Thanks!

CodePudding user response:

import Main is likely what you want. Just note that the variables from the Main module will need to be accessed as attributes of the Main object. That is, if a variable or function is called x in Main.py then it will be called Main.x after it's imported in this way.

CodePudding user response:

Try to delete the . before Main. Like this:

from Main import *

CodePudding user response:

Say you have following file structure:

GUI.py
    |
    |__
       folder
         |
         |__
            MAIN.py

So if you want to acces main.py, add a blank __init__.py in the folder of main.py. Then access it using:

from folder import MAIN as m

OR

import folder.MAIN as m

And if want to access the variable,

Access global variable in main.py using m.variable_name and local variable in side a function by m.function_name_variable_name, but note you have to write local variable in function like function_name.variable_name

def a():
    a.name="Faraaz"
a()
print(a.name)

       

CodePudding user response:

Where are your variables lie? If it lies outside some function, it should be Constant. If it lies inside the function, it is a different story.

Guess I have 2 files like this (maybe same as you have) enter image description here

My main.py has a Constant with the value "something" enter image description here

My gui.py has imported the main.py enter image description here

The Constant from main.py should works well in gui.py enter image description here

CodePudding user response:

You're misunderstanding the import, because its function is to actually run the python file. Two things:

Firstly, for your Main.py to be an actual module, you need to have in it a condition that tells the interpreter to do not run his stuff when importing it like when you call it directly from a shell:

if __name__ == '__main__':
    # put here everything you want to run when calling Main.py directly, but not when importing

Secondly, the variables you defined in Main.py are actually being imported, but you're probably trying to access them the wrong way. Look:

>>> import os
>>> os.SCHED_FIFO
4
>>> from os import *
>>> SCHED_FIFO
4
>>>

Be careful if you'll do an import * like above, because you will overwrite local objects if you have objects with the same name:

>>> SCHED_FIFO = 0
>>> SCHED_FIFO
0
>>> from os import *
>>> SCHED_FIFO
4
>>>
  • Related