Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Import / global name "" is not defined


  • Please log in to reply
1 reply to this topic

#1 Shadow21


    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 27 March 2011 - 10:11 AM

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):

			SettingsFile = open('settings.txt', 'r')
		except IOError:
			import CreateSettings
	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?
  • 0

#2 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 27 March 2011 - 01:41 PM

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

  • 0

The roots of education are bitter, but the fruit is sweet.

Also tagged with one or more of these keywords: import

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download