3

I feel really dumb asking this question, but it's a quirk of python I've put up with for awhile now that I finally want to fix.

On CentOS 7, given that I have "roflmao.py" and "__init__.py" in the directory:

 /usr/lib/python2.7/site-packages/roflmao

Why is it that when I'm using the python interpreter (and not in the directory containing roflmao.py), I must type:

from roflmao import roflmao

Instead of simply:

import roflmao

To gain access to "roflmao.py"'s functions and variables? I can import re, collections, requests, or any PIP-installed module just fine, but not my own custom one.

How can I set things up to accomplish this?

Locane
  • 2,886
  • 2
  • 24
  • 35
  • 1
    Have you take a look to [this](http://stackoverflow.com/a/4116384/3077939) ? – aluriak Sep 01 '16 at 20:04
  • Thanks Aluriak! That answer is basically what I need and I feel that DuckPuncher's is more specific and less buried. – Locane Sep 01 '16 at 20:10

1 Answers1

3

Put from roflmao import * into __init__.py.

If you do this, then you don't really need to use roflmao.py. Because it would then be pointless to do from roflmao import roflmao. So it's best to just put the code from roflmao.py into __init__.py.

RattleyCooper
  • 4,997
  • 5
  • 27
  • 43
  • I've been watching your edits - you're totally right. I ended up putting "from roflmao import *" in "\__init\__.py", and that totally worked! – Locane Sep 01 '16 at 20:05
  • Haha, sorry about that. – RattleyCooper Sep 01 '16 at 20:05
  • If you change your answer to "from roflmao import *" I'll set it as accepted. The way you've suggested currently still necessitates the "from" thing I was trying to avoid. – Locane Sep 01 '16 at 20:07