Ravil Asadov Ravil Asadov - 1 year ago 129
Python Question

How to avoid .pyc files using selenium webdriver/python while running test suites?

There's no relevant answer to this question. When I run my test cases inside a test suite using selenium webdriver with python the directory gets trashed with .pyc files. They do not appear if I run test cases separately, only when I run them inside one test suite.How to avoid them?

import unittest
from FacebookLogin import FacebookLogin
from SignUp import SignUp
from SignIn import SignIn

class TestSuite(unittest.TestSuite):
def suite():
suite = unittest.TestSuite()

return suite

if __name__ == "__main__":

Answer Source

pyc files are created any time you import a module, but not when you run a module directly as a script. That's why you're seeing them when you import the modules with the test code but don't see them created when you run the modules separately.

If you're invoking Python from the command line, you can suppress the creating of pyc files by using the -B argument. You can also set the environment variable PYTHONDONTWRITEBYTECODE with the same effect. I don't believe there's a way to change that setting with Python code after the interpreter is running.

In Python 3.2 and later, the pyc files get put into a separate __pycache__ folder, which might be visually nicer. It also allows multiple pyc files to exist simultaneously for different interpreter versions that have incompatible bytecode (a "tag" is added to the file name indicating which interpreter uses each file).

But even in earlier versions of Python, I think that saying the pyc files are "trashing" the directory is a bit hyperbolic. Usually you can exempt the created files from source control (e.g. by listing .pyc in a .gitignore or equivalent file), and otherwise ignore them. Having pyc files around speeds up repeated imports of the file, since the interpreter doesn't need to recompile the source to bytecode if the pyc file already has the bytecode available.

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