Export to GitHub

spyderlib - issue #771

import sitecustomize fails


Posted on Sep 26, 2011 by Helpful Camel

What steps will reproduce the problem? Start Spyder

What is the expected output? What do you see instead? the IPython window shows the following error message:

'import sitecustomize' failed; use -v for traceback

Traceback (most recent call last): File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\startup.py", line 227, in <module> ipythonshell.IP.stdin_encoding = os.environ['SPYDER_ENCODING'] File "C:\Python27\lib\os.py", line 423, in getitem return self.data[key.upper()] KeyError: 'SPYDER_ENCODING'

What version of the product are you using? On what operating system? Spyder 2.0.13 with python 2.7, on windows 7, 64 bit

Please provide any additional information below. This is the second time I get this error message. I can remediate by starting a new ipython interpreter, so it doesn't completely block me.

Comment #1

Posted on Oct 1, 2011 by Swift Elephant

I'm having the same problem with the same operating system.

Comment #2

Posted on Oct 2, 2011 by Happy Camel

Once v2.1.0beta3 is available as a Windows installer (tomorrow), you should try to upgrade to this new release to see if this bug has been fixed since then.

Is it happening with the standard Python interpreter?

Comment #3

Posted on Oct 2, 2011 by Happy Camel

Here is a way to debug this kind of problem: 1. Edit spyderlib/widgets/externalshell/pythonshell.py 2. Go to line #371: http://code.google.com/p/spyderlib/source/browse/spyderlib/widgets/externalshell/pythonshell.py?repo=v20#371 3. Change it to p_args = ['-u', '-v']

Restart Spyder and the console should print a lot of debug lines: among them, you'll prbably find the traceback associated to the error happening in sitecustomize.

Comment #4

Posted on Oct 4, 2011 by Swift Elephant

Output of -v attached.

Attachments

Comment #5

Posted on Oct 6, 2011 by Happy Camel

I think that part of the output is missing (the beginning) because I don't see any traceback except the KeyError but that does not happen with the sitecustomize script.

To avoid the output to be truncated, you should try to increase Console's history depth in preferences (note that this setting won't be applied dynamically on existing console tabs: you have to close the interpreter tab and open another one).

Comment #6

Posted on Oct 7, 2011 by Swift Elephant

I did as you suggested and the output looks exactly the same...

Comment #7

Posted on Oct 8, 2011 by Quick Horse

(No comment was entered for this change.)

Comment #8

Posted on Oct 10, 2011 by Swift Elephant

I decided to set an environment variable in Windows 7 to see if it would fix the problem. I made a variable named SPYDER_ENCODING and set it to UTF-8. I no longer get the error. Is there some problem that spyder has with windows 7's default encoding? Does anybody have any ideas?

Comment #9

Posted on Oct 11, 2011 by Happy Camel

And are you still getting the warning message "'import sitecustomize' failed; use -v for traceback" ?

Comment #10

Posted on Dec 25, 2011 by Helpful Cat

same issue. Mine is windows XP. Using NathanH...'s method, then it works.

Comment #11

Posted on Jan 16, 2012 by Swift Hippo

I had this problem in Windows 7 after upgrading to 2.17. Removed the .spyder folder and the problem was gone.

Comment #12

Posted on Mar 13, 2012 by Helpful Camel

I still have this issue. I updated to version 2.1.8 and I also tried to set an environment variable SPYDER_ENCODING and set it to UTF-8. It doesn't help.

Anything else I can try?

Comment #13

Posted on Mar 18, 2012 by Happy Camel

Please try debugging as suggested in comments #3 and #5.

Comment #14

Posted on Mar 20, 2012 by Helpful Camel

Pierre, I always get the error that .spyder.ini cannot be opened with 'w' or that the filename is not correct. However, I can open the file in that specific location, edit and save without any problem. Here's an example:

Traceback (most recent call last): File "C:\Python27\lib\site-packages\spyderlib\plugins\configdialog.py", line 164, in button_clicked configpage.apply_changes() File "C:\Python27\lib\site-packages\spyderlib\plugins\configdialog.py", line 71, in apply_changes self.save_to_conf() File "C:\Python27\lib\site-packages\spyderlib\plugins\configdialog.py", line 322, in save_to_conf self.set_font(font, option) File "C:\Python27\lib\site-packages\spyderlib\plugins__init__.py", line 222, in set_plugin_font set_font(font, self.CONF_SECTION, option) File "C:\Python27\lib\site-packages\spyderlib\config.py", line 461, in set_font CONF.set(section, option+'/bold', int(font.bold())) File "C:\Python27\lib\site-packages\spyderlib\userconfig.py", line 310, in set self.save() File "C:\Python27\lib\site-packages\spyderlib\userconfig.py", line 154, in __save conf_file = file(self.filename(),'w') IOError: [Errno 22] invalid mode ('w') or filename: 'C:\Users\RDC\.spyder2\.spyder.ini' Traceback (most recent call last): File "C:\Python27\lib\site-packages\spyderlib\plugins\configdialog.py", line 155, in accept configpage.apply_changes() File "C:\Python27\lib\site-packages\spyderlib\plugins\configdialog.py", line 71, in apply_changes self.save_to_conf() File "C:\Python27\lib\site-packages\spyderlib\plugins\configdialog.py", line 309, in save_to_conf self.set_option(option, checkbox.isChecked()) File "C:\Python27\lib\site-packages\spyderlib\plugins__init.py", line 210, in set_option CONF.set(self.CONF_SECTION, str(option), value) File "C:\Python27\lib\site-packages\spyderlib\userconfig.py", line 310, in set self.save() File "C:\Python27\lib\site-packages\spyderlib\userconfig.py", line 154, in __save conf_file = file(self.filename(),'w') IOError: [Errno 22] invalid mode ('w') or filename: 'C:\Users\RDC\.spyder2\.spyder.ini' Traceback (most recent call last): File "C:\Python27\lib\site-packages\spyderlib\plugins\configdialog.py", line 155, in accept configpage.apply_changes() File "C:\Python27\lib\site-packages\spyderlib\plugins\configdialog.py", line 71, in apply_changes self.save_to_conf() File "C:\Python27\lib\site-packages\spyderlib\plugins\configdialog.py", line 309, in save_to_conf self.set_option(option, checkbox.isChecked()) File "C:\Python27\lib\site-packages\spyderlib\plugins__init.py", line 210, in set_option CONF.set(self.CONF_SECTION, str(option), value) File "C:\Python27\lib\site-packages\spyderlib\userconfig.py", line 310, in set self.__save() File "C:\Python27\lib\site-packages\spyderlib\userconfig.py", line 154, in __save conf_file = file(self.filename(),'w') IOError: [Errno 22] invalid mode ('w') or filename: 'C:\Users\RDC\.spyder2\.spyder.ini'

I now tried to avoid saving, by adapting the userconfig.py as follows: def __save(self): """ Save config into the associated .ini file """ #this part of the script causes problems so let's try to save nothing at all #conf_file = file(self.filename(),'w') #self.write(conf_file) #conf_file.close() pass

And now I can open spyder again after it was closed (that's a start) and the first attempts show no failed sitecustomize import. Of course, I cannot save my preferences now, or does that happen in a different script?

roel

Comment #15

Posted on Jul 4, 2012 by Swift Elephant

I'm having the exact same problem. Oddly enough, it only happens sometimes (sometimes I get an IPython shell open without a problem, other times it throws this same error). I've attached debug details.

Attachments

Comment #16

Posted on Jul 6, 2012 by Quick Horse

Issue 1067 has been merged into this issue.

Comment #17

Posted on Jul 6, 2012 by Quick Horse

shoobs, I think you are having the same problem as the one reported in comment #14, i.e. .spyder.ini is not writable. We have seen this a couple of times before and it seems to be related to an antivirus problem.

Could you try to deactivate your antivirus and try again?

Comment #18

Posted on Jul 7, 2012 by Happy Panda

Well I use linux, and there is no antivirus ;)

Comment #19

Posted on Jul 7, 2012 by Quick Horse

But are you seeing the exact same error as the one reported in comments #14 and #15? I say it because it seems this problem is caused by different reasons.

Please follow the procedure described in comment #3 to let us know.

Comment #20

Posted on Jul 9, 2012 by Swift Elephant

ccordoba12, I added an exception to my antivirus for .spyder.ini, and I have yet to see the error again. Thanks!

Comment #21

Posted on Jul 28, 2012 by Happy Camel

(No comment was entered for this change.)

Comment #22

Posted on Sep 6, 2012 by Happy Bird

I'm on Windows XP and I'm also getting this " 'import sitecustomize' failed; use -v for traceback " from Spyder 2.1.9 This seems to have fixed it. 1. Goto C:\Python27\Lib\site-packages\spyderlib\widgets\externalshell\ (or the equivalent) 2. Delete sitecustomize.pyc (make sure sitecustomize.py is still there, don't delete that). This alone may be sufficient. But the 3rd step I took. 3. Assuming you installed with Python(x,y), The Start Menu under Programs has Python(x,y) > Optimize Python Modules > select the site-packages directory. This creates a new sitecustomize.pyo and pyc from the sitecustomize.py That seems to have made the problem go away.

Comment #23

Posted on Feb 11, 2013 by Quick Lion

22 steps 1-2 worked for me on Win 7 (64b).

Thanks!

Comment #24

Posted on Mar 13, 2013 by Happy Camel

Issue 1310 has been merged into this issue.

Comment #25

Posted on Mar 13, 2013 by Happy Ox

Deleting sitecustomize.pyc and sitecustomize.pyo didn't work for me but setting SPYDER_ENCODING variable:

set SPYDER_ENCODING=utf-8

Comment #26

Posted on Mar 13, 2013 by Grumpy Hippo

I have the" 'import sitecustomize' failed; use -v for traceback" issue that prevent me to use ipython from spyder. Using latest spyder and ipython from respective repo.

The problem looks related to sys.argv not being available anymore for ipython when it is "called" from spyder:

Traceback (most recent call last): File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py", line 510, in execsitecustomize import sitecustomize File "/Users/dt/lib/python/spyderlib/widgets/externalshell/sitecustomize.py", line 298, in from IPython.core.debugger import Pdb as ipyPdb File "/Users/dt/lib/python/IPython/init.py", line 43, in from .config.loader import Config File "/Users/dt/lib/python/IPython/config/init.py", line 16, in from .application import * File "/Users/dt/lib/python/IPython/config/application.py", line 67, in """.strip().format(app=os.path.basename(sys.argv[0]))

Comment #27

Posted on Mar 15, 2013 by Happy Camel

@klonuo: this means that sitecustomize.py is failing elsewhere.

Please follow the instructions of comment #3 with the new link: https://code.google.com/p/spyderlib/source/browse/spyderlib/widgets/externalshell/pythonshell.py#391

Comment #28

Posted on Mar 15, 2013 by Happy Ox

Pierre,

here is log: http://pastebin.com/raw.php?i=wMrrwH2A

In the first traceback it seems like someone is passing this "wx4agg" argument (I don't have MATPLOTLIB_BACKEND environment variable defined)

In second traceback is this issue about SPYDER_ENCODING variable

So first traceback is responsible for raising 'import sitecustomize' failed; use -v for traceback and second traceback is responsible for failing to bring IPython console

Comment #29

Posted on Mar 15, 2013 by Happy Ox

ha ha, that someone that's passing this faulty 'wx4agg' value is my .spyder.ini

I deleted the file and there is no single issue now. Both errors gone.

This file was first created with some previous version. Did I somehow set 'wx4agg' value, I have no idea

Comment #30

Posted on Apr 7, 2013 by Grumpy Hippo

Still not able to launch a ipython from spyder (lastest spyder release candidate):

/Users/dtremoui/lib/python/IPython/utils/io.pyc matches /Users/dtremoui/lib/python/IPython/utils/io.py

import IPython.utils.io # precompiled from /Users/dtremoui/lib/python/IPython/utils/io.pyc

/Users/dtremoui/lib/python/IPython/utils/traitlets.pyc matches /Users/dtremoui/lib/python/IPython/utils/traitlets.py

import IPython.utils.traitlets # precompiled from /Users/dtremoui/lib/python/IPython/utils/traitlets.pyc Traceback (most recent call last): File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py", line 510, in execsitecustomize import sitecustomize File "/Users/dtremoui/Documents/devel_logiciel/external_used/spyderlib/spyderlib/widgets/externalshell/sitecustomize.py", line 298, in from IPython.core.debugger import Pdb as ipyPdb File "/Users/dtremoui/lib/python/IPython/init.py", line 43, in from .config.loader import Config File "/Users/dtremoui/lib/python/IPython/config/init.py", line 16, in from .application import * File "/Users/dtremoui/lib/python/IPython/config/application.py", line 67, in """.strip().format(app=os.path.basename(sys.argv[0])) AttributeError: 'module' object has no attribute 'argv' Python 2.7.3 (default, Oct 23 2012, 10:31:53) [GCC 4.2.1 Compatible Apple Clang 2.1 (tags/Apple/clang-163.7.1)] on darwin Type "help", "copyright", "credits" or "license" for more information. import IPython # directory /Users/dtremoui/lib/python/IPython

/Users/dtremoui/lib/python/IPython/init.pyc matches /Users/dtremoui/lib/python/IPython/init.py

import IPython # precompiled from /Users/dtremoui/lib/python/IPython/init.pyc import IPython.config # directory /Users/dtremoui/lib/python/IPython/config

Comment #31

Posted on Apr 7, 2013 by Quick Horse

This seems an error with your IPython installation, given this line in your traceback:

File "/Users/dtremoui/lib/python/IPython/config/application.py", line 67, in """.strip().format(app=os.path.basename(sys.argv[0])) AttributeError: 'module' object has no attribute 'argv'

what IPython version are you using?

Comment #32

Posted on Apr 7, 2013 by Grumpy Hippo

I'm on Mac Os X 10.7.5 . I've tested both with ipython 0.13.2 and latest head: same traceback. (Python 2.7.3 64bits, Qt 4.8.4, PyQt4 (API v2) 4.10 on Darwin) Any recommendation how I could dig in this issue ?

Comment #33

Posted on Apr 8, 2013 by Quick Horse

This is my advice:

  1. Try to run IPython qtconsole to see if it's working correctly. Just fire up Terminal.app and write

ipython qtconsole

  1. Open our sitecustomize file

/Users/dtremoui/Documents/devel_logiciel/external_used/spyderlib/spyderlib/widgets/externalshell/sitecustomize.py

and comment all lines that are giving you trouble, like line 298, to see if you can make IPython to work. I think you won't be able to, but it could help you to narrow down the source of the error.

Comment #34

Posted on Apr 9, 2013 by Grumpy Hippo
  1. For sure it works. I use ipython qtconsole and notebooks almost every day.
  2. Thanks for this clue, I'll report here as soon as I could narrow something down...

Comment #35

Posted on Apr 9, 2013 by Grumpy Hippo

IPython console does start and run properly after commenting line 298 and 299 ./spyderlib/spyderlib/widgets/externalshell/sitecustomize.py That solve all the problem for me (but I don't know what happen for pdb as I almost never use it).

I'm still not able to understand why the hell sys.argv is not available when "running" IPython/config/application.py This part of the coede is called three times while running spyder and asking for the qtconsole, but it failled only once (the one I reported, see #30)...

Comment #36

Posted on Apr 23, 2013 by Quick Horse

I have no idea where those "argv" errors are coming from. But I could put the lines you refer to in a try/except block so that IPython can start without problems for you.

Comment #37

Posted on Apr 24, 2013 by Grumpy Hippo

Well, thanks for that proposal. I don't thing it really make sense if I'm the only person who suffer from this issue. It's not a big deal to systematically patch a new version for this issue locally on my computer. Furthermore, that will regularly remind me to spend some time to solve this one ;-) So lets wait and see if anybody else encounter the same issue to spend more time on it. Thanks you again for your help !

Comment #38

Posted on May 18, 2013 by Helpful Elephant

I also had the same issue as david.trem ('module' object has no attribute 'argv'). My environment is somewhat different (Python 2.7.2 32bit on Windows), but I'm also using the latest stable versions of both Spyder (2.2.0) and IPython (0.13.2).

It seems like the sys module does not contain the argv object. This seems to happen when you create a python interpreter from the C API (e.g. when embedding) and don't initialize it with any arguments, so it's supposed to be expected behavior, but maybe modules - including IPython, it seems - are not aware of it and try to access the argv attribute anyway.

The way I fixed it is rather silly:

  1. I opened spyderlib/widgets/externalshell/sitecustomize.py
  2. Looked for the line where IPython is imported from. In my case it was line 292 ('from IPython.core.debugger import Pdb as ipyPdb')
  3. Added a dummy access to sys.argv before that line:

    • sys.argv # Dummy access, reads sys.argv, but do nothing with it. For whatever reason this makes the sys.argv attribute available for IPython from IPython.core.debugger import Pdb as ipyPdb pdb.Pdb = ipyPdb

Not a very elegant solution, but it easily fixes this issue with no side effects.

Comment #39

Posted on May 18, 2013 by Helpful Elephant

Ok, my previous solution doesn't seem to fix this error completely it only gets the throw-site moved to a context where it's caught and handled properly, but site customization is not completed properly, so IPython<->Spyder integration is missing. I've solved this by properly creating an empty argv object inside the 'sys' module:

  • if not hasattr(sys, 'argv'):
  • sys.argv = [''] from IPython.core.debugger import Pdb as ipyPdb pdb.Pdb = ipyPdb

I've also attached a proper patch file.

Attachments

Comment #40

Posted on Jul 2, 2013 by Quick Horse

Thanks a lot for the patch! I encountered the problem while testing with Python 3 and it effectively solved the problem.

Comment #41

Posted on Jul 6, 2013 by Grumpy Rhino

This was annoying me too. My solution was to install matplotlib, python-dateutil and pyparsing.

Reason: there is a matplotlib import above the last suggested fix, with a comment that the IPython import will fail if matplotlib isn't imported first. Without actually looking into the detail, I assume that matplotlib import is setting sys.argv somewhere so that ipdb won't complain.

Comment #42

Posted on Feb 17, 2015 by Quick Horse

This issue was migrated to https://github.com/spyder-ide/spyder/issues/

The issue number is exactly the same

Status: Done

Labels:
Type-Defect Priority-Medium Cat-Console Restrict-AddIssueComment-Commit