I wrote an import random code, and unfortunately saved it as the module's name I was trying to call. eg. random.py

I believe, but am not sure, that I created the random.py file twice and thus I deleted the random.pyc, twice, that popped up in the file holding all my python programs, after running import random in the terminal, twice.

If I didn't create the random.py twice, somehow another random.pyc (besides the first one) was in the file holding my programs and I deleted it.

All the people I asked told me to delete the .pyc, and any file with random, random.pyo as its name. I've checked all my user created files, and none of them fit the description.

One of them told me to find and clear the temporary cached version of the random.pyc and random.pyo.

My question is where on this MAC air Yosemite can I locate a temporary cached version, and how to safely clear it?

  • The directory .pyc files are created in depends on the version of python you are using. Which version?
    – mmmmmm
    Commented Jun 29, 2015 at 21:33
  • @Mark python 2.7.6
    – Red
    Commented Jun 29, 2015 at 22:54

3 Answers 3


use mdfind to locate all the files with that name (you might need to grep -w for the string or use mdfind with a "kMDItemFSName == whatever.py" clause)

Once you know where you've sprayed those files, you could either edit it into the question or decide to do a clean install / migrate user data or remove all the python and then patch that together from a clean install on an external drive.

It's had to know what specific things you've done, but hopefully a few of the tools can narrow you down to some options.

If I were on deadline and just needed to know what version of python was running - I'd brew install python after setting up https://brew.sh

You'd then have three installations, but the one that runs from the shell is controllable with $PATH and you can be sure it's up to date with homebrew.

  • I'm like a few days new to python, and programming in general. I used your commands and someone else's to find the original .py, .pyo, .pyc and no duplicates. My problem was resolved by simply going to the path that led to the file, starting python, and calling the module. I will backup my system, immediately, though because I was thinking this entire time I screwed up the Python on my Mac. And next time, I will make my post clearer. Thanks a bunch bmike.
    – Red
    Commented Jun 29, 2015 at 21:19
  • @Red Awesome! Welcome and I'm glad you're learning python and the site. Go ahead and answer your question and then accept it after the waiting period is past. That will help others that are new much more than an "expert's" answer will since different abilities need different levels of detail.
    – bmike
    Commented Jun 29, 2015 at 21:20
  • Got it. Should I remove that edit.
    – Red
    Commented Jun 29, 2015 at 22:30

For python 2.x all files related to the random.py file you created will be in the same directory as this file.

For python 3.4 see this section about compiled python files of the python docs

To speed up loading modules, Python caches the compiled version of each module in the __pycache__ directory under the name module.version.pyc, where the version encodes the format of the compiled file; it generally contains the Python version number. For example, in CPython release 3.3 the compiled version of spam.py would be cached as __pycache__/spam.cpython-33.pyc. This naming convention allows compiled modules from different releases and different versions of Python to coexist.


Final Answer:

Hey new programmers to python.

I had a problem of importing the built-in module 'random'.

  1. Make sure you do not name your program after the module.

    a. If you do, you have to delete the newly created random.pyc or other
    random. file name duplicates in the file which holds the random.py you created. These are GUI files that can be placed in trashcan. This step should solve 100% of your file duplicate problems if you have python

    Learn to recognize the original files. From a mistake of my own I found the path to the module in the Python standard library. MAC Path just before module is named:


MAC Path to random module: /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7

Use 'cd ..', 'ls', and 'cd' to get to python2.7 and enter 'ls' and you 
will see a lot of original files with .py, .pyo, .pyc endings. This is  
the location that they only should be in other than a similar path but 
in a different python version.)

 i. If you still think you have newly created files floating about in 
 the library use command;

      find ./python2.7 -name random.* -ls

 This will list the date of all files that start with 'random.' and   
 if there any have dates that do not match with the original 
 installation date or update dates, that is the file that is 
 causing the problem. 

 ii. You can also type this command into terminal to find   
 files but it doesn't list the time of installation;

  mdfind kMDItemFSName == random.py

 Obviously, since you know what the original file path looks like, 
 you'll be able to not target the wrong file!
  1. Once you eliminated new files, rename your program anything other than random.py and possibly anything other than reserved words in python. I called mine generator.py

  2. Make sure when in terminal you are in the right path before you start 'python', and type 'import generator' (or what the program is called).

To get to the right path use 'ls', 'cd' commands to get to the path that ends with the file that holds the program. (This was the last block to getting the import to work - I spent at least 6 hrs agonizing I had other hidden newly created .pyc files even when the command searches I had done above yielded none.

You find yourself blocked, take a break, and come back to it. Something clicked in my head after looking at several places online including stackflow, python.org (https://docs.python.org/2/tutorial/modules.html), and all this talk about paths.

I was never in the right path before I imported!!

  1. Start 'python'

  2. Enter 'import generator'

You should get an output of 10 numbers from 0.0 to 1.0

I answered how to find new versions of random.py, random.pyc, and random.pyc. I partially answered how to clear the new ones, especially the ones you can manually dump in your trashcan. I still do not know how to find temporary cached version of the files because apparently that wasn't the problem! The problem was I named my program a bad name, and I was not in the right path before I started python and importing.

  • @ Check out Mark's answer to where these .pyc will be based on the version of python you have. My answer is for python 2.x.
    – Red
    Commented Jun 29, 2015 at 23:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .