1,105,625 Community Members

Do not display Warning

Member Avatar
Musing888
Newbie Poster
17 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

When I run my code the program adds the following before the start of my program:

>>>
Warning (from warnings module):
File "C:/Users/Steven/Documents/CA/CA python/CAMain.py", line 142
def maxgen(fname, worksheet=1,encoding='cp1251'):
SyntaxWarning: import * only allowed at module level
>>>
The first prompt of my program would be here


The code it refers to is the following:

def maxgen(fname, worksheet=1,encoding='cp1251'):
    from pyExcelerator import *

The program works fine, but I don't want it to display the warning for each function that includes "import *" as there are several.

Any ideas? Thanks in advance.

Member Avatar
Beat_Slayer
Posting Pro in Training
401 posts since Jun 2010
Reputation Points: 17 [?]
Q&As Helped to Solve: 105 [?]
Skill Endorsements: 1 [?]
 
0
 

Here it is.

import warnings

def maxgen(fname, worksheet=1,encoding='cp1251'):
    warnings.filterwarnings('ignore')
    from pyExcelerator import *
    warnings.filterwarnings('always')

Happy coding...

Member Avatar
snippsat
Veteran Poster
1,041 posts since Aug 2008
Reputation Points: 483 [?]
Q&As Helped to Solve: 382 [?]
Skill Endorsements: 10 [?]
 
0
 

from pyExcelerator import *

Import shall always be on the top,not inside a function.
That`s why you get this warning.
SyntaxWarning: import * only allowed at module level

DON'T do this:
from pyExcelerator import *
unless you are absolutely sure that you will use each and every thing in that module.
And even then, you should probably reconsider using a different approach.

Better.
from pyExcelerator import <something>
Then use it like this: something()

import pyExcelerator as Ex
Then use it like this: Ex.something()

import pyExcelerator
Then use it like this: pyExcelerator.something()

Example:

#dir(random) #show functions/methods in random
>>> from random import randrange
>>> randrange(1,10)
3
>>> import random as R
>>> R.randrange(5,14)
12
>>> import random
>>> random.randrange(2,8)
6
Member Avatar
Musing888
Newbie Poster
17 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Here it is.

import warnings

def maxgen(fname, worksheet=1,encoding='cp1251'):
    warnings.filterwarnings('ignore')
    from pyExcelerator import *
    warnings.filterwarnings('always')

Happy coding...

I used the following code within each of my functions as well as the main part of the program:

def maxgen(fname, worksheet=1,encoding='cp1251'):
    import warnings
    warnings.filterwarnings('ignore')
    from pyExcelerator import *
    warnings.filterwarnings('always')

Unfortunately, I received the same syntax warning. Any ideas why?

Member Avatar
Musing888
Newbie Poster
17 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Import shall always be on the top,not inside a function.
That`s why you get this warning.
SyntaxWarning: import * only allowed at module level

DON'T do this:
from pyExcelerator import *
unless you are absolutely sure that you will use each and every thing in that module.
And even then, you should probably reconsider using a different approach.

Better.
from pyExcelerator import <something>
Then use it like this: something()

import pyExcelerator as Ex
Then use it like this: Ex.something()

import pyExcelerator
Then use it like this: pyExcelerator.something()

Example:

#dir(random) #show functions/methods in random
>>> from random import randrange
>>> randrange(1,10)
3
>>> import random as R
>>> R.randrange(5,14)
12
>>> import random
>>> random.randrange(2,8)
6

Is there anyway from looking at my code that I can determine which items I need to import from pyExcelerator?

Member Avatar
snippsat
Veteran Poster
1,041 posts since Aug 2008
Reputation Points: 483 [?]
Q&As Helped to Solve: 382 [?]
Skill Endorsements: 10 [?]
 
0
 
def foo():
    from random import *
    print randrange(5,8)

foo()
#--><module1>:1: SyntaxWarning: import * only allowed at module level
5

from random import randrange
def foo():
    from random import *
    print randrange(5,8)

foo()
#-->6

Is there anyway from looking at my code that I can determine which items I need to import from pyExcelerator?

Use dir(pyExcelerator)

>>> import random
>>> dir(random)
['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', 'WichmannHill', '_BuiltinMethodType', '_MethodType', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_acos', '_ceil', '_cos', '_e', '_exp', '_hexlify', '_inst', '_log', '_pi', '_random', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'division', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'jumpahead', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']
>>> help(random.shuffle)
Help on method shuffle in module random:

shuffle(self, x, random=None, int=<type 'int'>) method of random.Random instance
    x, random=random.random -> shuffle list x in place; return None.
    
    Optional arg random is a 0-argument function returning a random
    float in [0.0, 1.0); by default, the standard random.random.

>>>
Member Avatar
Musing888
Newbie Poster
17 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
def foo():
    from random import *
    print randrange(5,8)

foo()
#--><module1>:1: SyntaxWarning: import * only allowed at module level
5

from random import randrange
def foo():
    from random import *
    print randrange(5,8)

foo()
#-->6

Use dir(pyExcelerator)

>>> import random
>>> dir(random)
['BPF', 'LOG4', 'NV_MAGICCONST', 'RECIP_BPF', 'Random', 'SG_MAGICCONST', 'SystemRandom', 'TWOPI', 'WichmannHill', '_BuiltinMethodType', '_MethodType', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_acos', '_ceil', '_cos', '_e', '_exp', '_hexlify', '_inst', '_log', '_pi', '_random', '_sin', '_sqrt', '_test', '_test_generator', '_urandom', '_warn', 'betavariate', 'choice', 'division', 'expovariate', 'gammavariate', 'gauss', 'getrandbits', 'getstate', 'jumpahead', 'lognormvariate', 'normalvariate', 'paretovariate', 'randint', 'random', 'randrange', 'sample', 'seed', 'setstate', 'shuffle', 'triangular', 'uniform', 'vonmisesvariate', 'weibullvariate']
>>> help(random.shuffle)
Help on method shuffle in module random:

shuffle(self, x, random=None, int=<type 'int'>) method of random.Random instance
    x, random=random.random -> shuffle list x in place; return None.
    
    Optional arg random is a 0-argument function returning a random
    float in [0.0, 1.0); by default, the standard random.random.

>>>

I looked through my code and found everything listed in dir(pyExcelerator). No more error messages or warnings! Thanks so much.

Question Answered as of 3 Years Ago by snippsat and Beat_Slayer
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: