Jump to content

Import / global name "" is not defined

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Shadow21

Shadow21

    Newbie

  • Members
  • PipPip
  • 12 posts
I'm using Python 3 and I'm trying to refine my program so I'm trying to use

from os import system

from subprocess import getoutput

from time import sleep

instead of

import os, subprocess, time

but when I try to import from a module I'm getting this error when I run my program:

NameError: global name 'os' is not defined

This is the part of the code that I'm using os in:

from os import system

from subprocess import getoutput

from time import sleep


class FanControl:


	def __init__(self):


		try:

			SettingsFile = open('settings.txt', 'r')

		except IOError:

			import CreateSettings

		else:

			FanControl.EnableFanSettings(SettingsFile)

			

	def EnableFanSettings(SettingsFile):


		os.system('nvidia-settings -a [gpu:0]/GPUFanControlState=1')

		os.system('nvidia-settings -a [gpu:1]/GPUFanControlState=1')

		os.system('nvidia-settings -a [gpu:2]/GPUFanControlState=1')

		os.system('nvidia-settings -a [gpu:3]/GPUFanControlState=1')

Why am I getting an error when I import a function from a module but not when I import the entire module?

#2
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 890 posts
  • Location:::1
When you say

from os import system

it means, you don't need (mustn't?) write os in front of system. Also, in your EnableFanSettings method, you should have self as first parameter, then the file one and in your constructor, else clause, I think there should be self and not FanControl (maybe it has the same meaning, but I was told to use self).

Edited by Flying Dutchman, 27 March 2011 - 02:04 PM.
lied about 'self' as method argument

A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users