0

i'm a beginner with Python and want to write a programm where i have a main program and several subprograms.

The problem i have is the following:

import func_betriebssysteme
func_betriebssysteme.betriebssystem()
    
# Schritt 1

print("Betriebssystem:",betriebssystem_use)

That is the code of "func_betriebssysteme", my separat py.file:

def betriebssystem():
import sys

temp_a = sys.platform

global betriebssystem_use
betriebssystem_use = str(temp_a)

And that is the errormessage:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_6860/1431564812.py in ()
      9 # Schritt 1
     10 
---> 11 print("Betriebssystem:",betriebssystem_use)

NameError: name 'betriebssystem_use' is not defined

If i have the function betriebssystem() directly at the beginning of the code and not in a separat file, it works.

What do i do wrong?

Edit 21.3.2023

Hello again,

I haven't really made any progress yet.

I'm still feeling my way along, bit by bit. In the process, I noticed the following.

When I run this code, I get a name error bsuse not defined.

from func_ausschr.func_betriebssysteme import *
betriebssystem()
print(bsuse)

That is the code in the file func_auschr.func_betriebssysteme

def betriebssystem():
    
    import sys
    global bsuse
    temp = sys.platform
    bsuse = temp

If I run the code again directly, the variable is resolved with win32.

What is going wrong here?

By the way, I use Python3.10 in Jupyter-Notebook (Visual Studio Code) for testing.

robebo
  • 13
  • 4
  • Does this answer your question? [Importing files from different folder](https://stackoverflow.com/questions/4383571/importing-files-from-different-folder) – Marcelo Paco Mar 18 '23 at 19:17
  • No, I'm sorry. The files are all in the same directory. I created a blank __init__.py in this folder. But it don't worked. Than i copied the both files (main-py and function-py) in the Python-folder. I don't works to. – robebo Mar 19 '23 at 11:18
  • I found this [link]https://iq-inc.com/importerror-attempted-relative-import/ and changed my structure of the directorys. Now i have a directory in which is my main-program and a subdirectory with an an empty '__init.py__ file' where my modules are stored. I understood this as the meaning of using a package. In my main code i import the module now with "import func_ausschr.func_betriebssysteme". But i get the same errormessage "NameError: name 'betriebssystem_use' is not defined" – robebo Mar 20 '23 at 12:28

0 Answers0