0

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

        suite.addTest(FacebookLogin("test_FacebookLogin"))
        suite.addTest(SignUp("test_SignUp"))
        suite.addTest(SignIn("test_SignIn"))
        return suite

if __name__ == "__main__":
    unittest.main()
Ravil Asadov
  • 65
  • 1
  • 7
  • iiSeymour , could you please help me with the question? Thanks for the edit, but my point was that I cannot find a relevant answer to this question. – Ravil Asadov Oct 06 '16 at 22:49
  • Are you using Python 2 or Python 3? – sytech Oct 06 '16 at 22:52
  • Possible duplicate of [How to avoid pyc files](http://stackoverflow.com/questions/154443/how-to-avoid-pyc-files) – sytech Oct 06 '16 at 22:54
  • I am using Python 2. It is not a duplicate, I have researched and that answer does not contain any specific details that I need. I also need to know where to insert that peace of code if any. – Ravil Asadov Oct 07 '16 at 17:27

2 Answers2

1

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.

Blckknght
  • 100,903
  • 11
  • 120
  • 169
  • The command that I run is: python LoginSuite.py and then it runs the test cases in the suite. I am not too savvy, please could you tell how I could use -B argument together with my command? – Ravil Asadov Oct 07 '16 at 17:38
  • Put the `-B` between the command and the script name (`python -B LoginSuite.py`) and it should work. – Blckknght Oct 07 '16 at 19:05
  • Thanks a lot, it worked for me, and does exactly what I needed. – Ravil Asadov Oct 07 '16 at 20:32
1

You can supply the -B option to the interpreter stop the files from being generated. See: How to avoid pyc files

If you really wanted to, you could also add to your test script a cleanup after running the test. Not that I'm recommending that.

import os
pyc_files = [fn for fn in os.listdir(os.getcwd()) if fn.endswith(".pyc")]
for filename in pyc_files:
    os.remove(filename)
Community
  • 1
  • 1
sytech
  • 29,298
  • 3
  • 45
  • 86
  • Thanks a lot for your effort and help. I used the **-B** together with the command on the Terminal `python -B LoginSuite.py` and it just did what I needed. Thanks – Ravil Asadov Oct 07 '16 at 20:36