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.

Here it is.

import warnings

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

Happy coding...

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

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?

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?

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.

>>>
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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.