Search code examples
pythonwindowspyflakes

How can I get pyflakes to run on Windows?


Having trouble getting pyflakes to run on windows.

On Windows there is no apt install , so... what to do?


Solution

  • I downloaded the pyflakes tgz file (v0.5.0) from Pypi, and was able to unpack it with a tar tool.

    Now I had a directory that looked like this:

    C:\temp>dir pyflakes-0.5.0
     Volume in drive C has no label.
     Volume Serial Number is A421-40B4
    
     Directory of C:\temp\pyflakes-0.5.0
    
    07/11/2012  06:28 PM    <DIR>          .
    07/11/2012  06:28 PM    <DIR>          ..
    09/03/2011  10:09 AM    <DIR>          bin
    07/11/2012  06:28 PM    <DIR>          build
    09/03/2011  09:02 AM             1,057 LICENSE
    09/03/2011  09:02 AM             1,696 NEWS.txt
    09/03/2011  10:09 AM               763 PKG-INFO
    09/03/2011  10:09 AM    <DIR>          pyflakes
    09/03/2011  09:30 AM             1,075 setup.py
                   4 File(s)          4,591 bytes
                   5 Dir(s)  74,326,130,688 bytes free
    

    Next I tried the obvious: run the setup.py file. It told me to use install, so

    \python27\python.exe setup.py install 
    

    That gave me a happy message about building and installing the module, culminating in

     Writing C:\Python27\Lib\site-packages\pyflakes-0.5.0-py2.7.egg-info
    

    ok, now ...

    (dinner break)

    now, the install of pyflakes puts a file called "pyflakes" into c:\Python27\Scripts.

    To run the pyflakes checker, use this:

    \Python27\python.exe \Python27\Scripts\pyflakes myfile.py
    

    That's it! It works.


    But there is more to the story. The contents of the file c:\Python27\Scripts\pyflakes is python source code. Now, you may think, what's the deal with a python module with no .py extension? Other files in that directory are correctly marked with the .py extension. It just feels wrong to leave the extension off. Why would they do that? If you get creative and rename that file called "pyflakes" to "pyflakes.py", and then try to run that, you will get this error:

    C:\dev\python>\Python27\python.exe \Python27\Scripts\pyflakes.py import1.py
    Traceback (most recent call last):
      File "\Python27\Scripts\pyflakes.py", line 3, in <module>
        from pyflakes.scripts.pyflakes import main
      File "C:\Python27\Scripts\pyflakes.py", line 3, in <module>
        from pyflakes.scripts.pyflakes import main
    ImportError: No module named scripts.pyflakes
    

    The problem is a naming clash between the (renamed) pyflakes.py file, and the module that it imports, which is located at c:\Python27\Lib\site-packages\pyflakes. So, don't rename it that way. If you insist on having a .py extension, then choose something other than pyflakes.py for the name - for example, pyflakes_driver.py works just fine. Some people use runpyflakes.py .

    If you do run into the problem described above, you also need to remove the pyflakes.pyc that may have been created in the c:\Python27\Scripts directory.


    People might also want to create a .bat or .cmd file to launch pyflakes. To help with this I created c:\Python27\pyflakes.cmd, with these contents:

    @echo off
    SETLOCAL
    if  _%1==_ goto USAGE
    %~dp0\python.exe %~dp0\Scripts\pyflakes %*
    goto ALL_DONE
    
    :USAGE
      echo usage:  pyflakes ^<file^> [^<optionalargs^>]
      echo run the pyflakes syntax checker on a python source file.
    
    :ALL_DONE
    ENDLOCAL
    

    ok, that's it.