Repository: ipython/ipython

Issue:
import tab-complete fail with ipython 2.0 shell
In the shell, when trying to tab-complete a module name for import, the propose completion is not modules names. 

with ipython 2.0

```
>>> import co<tab>
coerce(     compile(    complex(    continue    copyright(
```

That the same list as when tab-complete without anything

instead of what ipython 1.1 give

```
>>> import co<tab>
code                codeop              commands            configparser        contextlib          copy_reg
code_erreur         collections         compileall          constantes          cookielib           copyreg
codecs              colorsys            compiler            constantes_vivaldi  copy                coverage
```

I'm on fedora 17, with python 2.7 in a virtualenv



Discussion:
I am also seeing this. I'm using OSX and Python 2.7 provided by brew. Works fine with IPython 1.2.1 and earlier.

A user reported the same bug under Mageia with Python2.7 and Python3.3 

So far I'm unable to replicate this (Debian, Python 3.3 and 2.7), and unsure what could be causing it - I don't think that code has had any major changes since 1.x.

@GhislainHivon @drkjam do you have PYTHONSTARTUP set?

Ok, some more info here. I have PYTHONSTARTUP variable defined. 

 echo $PYTHONSTARTUP
/Users/mariogrgic/.pythonrc
$
$ cat .pythonrc
import rlcompleter
import readline

readline.parse_and_bind("tab: complete")
del rlcompleter
del readline

If I now unset PYTHONSTARTUP 

$ unset PYTHONSTARTUP

and launch IPython, import completions now work. So it does have something to do with PYTHONSTARTUP variable or what the startup script is doing.

IPython now runs PYTHONSTARTUP, and is less aggressive about using its own completer. So when you configure a more basic completer in PYTHONSTARTUP, IPython ends up using that instead of its own completer.

Ok, that explains it. I'm not sure if this is not working for the same reason for other people experiencing this problem.

Same thing, PYTHONSTARTUP with readline.parse_and_bind("tab: complete") in it. 

I found a easy way to test if in ipython
http://stackoverflow.com/a/17826459/611741

```
try:
     cfg = get_ipython().config
     # in pyton
 except :
     pass 
```

The name `get_ipython` should exist if it's run from IPython.


Resolving patch:
diff --git a/IPython/core/shellapp.py b/IPython/core/shellapp.py
--- a/IPython/core/shellapp.py
+++ b/IPython/core/shellapp.py
@@ -358,9 +358,23 @@ def _run_startup_files(self):
         """Run files from profile startup directory"""
         startup_dir = self.profile_dir.startup_dir
         startup_files = []
-        if self.exec_PYTHONSTARTUP and not (self.file_to_run or self.code_to_run or self.module_to_run):
-            if os.environ.get('PYTHONSTARTUP', False):
-                startup_files.append(os.environ['PYTHONSTARTUP'])
+        
+        if self.exec_PYTHONSTARTUP and os.environ.get('PYTHONSTARTUP', False) and \
+                not (self.file_to_run or self.code_to_run or self.module_to_run):
+            python_startup = os.environ['PYTHONSTARTUP']
+            self.log.debug("Running PYTHONSTARTUP file %s...", python_startup)
+            try:
+                self._exec_file(python_startup)
+            except:
+                self.log.warn("Unknown error in handling PYTHONSTARTUP file %s:", python_startup)
+                self.shell.showtraceback()
+            finally:
+                # Many PYTHONSTARTUP files set up the readline completions,
+                # but this is often at odds with IPython's own completions.
+                # Do not allow PYTHONSTARTUP to set up readline.
+                if self.shell.has_readline:
+                    self.shell.set_readline_completer()
+        
         startup_files += glob.glob(os.path.join(startup_dir, '*.py'))
         startup_files += glob.glob(os.path.join(startup_dir, '*.ipy'))
         if not startup_files:


Test patch: