Why is my .pythonrc file being run in non-interactive programs? - python

Context: I started using OSX about a year ago, and I had a kind of screwy python installation. That is, I was using system python, and installed packages with sudo when that seemed to make things work. Now, I'm starting from a fresh OSX install, and trying to do it the Right Way. I've installed python and python3 from brew, and trying to use python3 whenever possible.
Problem: I have a .pythonrc file, which just imports a handful of commonly used packages - mostly standard lib, a few popular nonstandard packages, and a few of my own. In the past, this file has only been run when I start an interactive shell. Now, when using brew python, it is run whenever I run any python program.
There must be some gap in my understanding of the rc file - I thought the purpose was specifically for interactive use. Still, when I use system python, the rc file isn't used - so something is different about my system python (2.7.10 at /usr/bin/python) vs brew python (2.7.12 at /usr/local/bin/python; 3.5.2 at /usr/local/bin/python3). The behavior is the same if I remove everything except a print statement from the rc file.
edit: I realized that the rc file is running because I'm importing ipdb. This makes sense I suppose, but I still don't understand why that would happen in some of the python environments/versions but not others.
edit: https://github.com/gotcha/ipdb/blob/master/ipdb/main.py#L44 this line fails, not sure if this means anything.
Full stacktrace from within .pythonrc:
File "hello.py", line 1, in <module>
from ipdb import set_trace
File "/usr/local/lib/python2.7/site-packages/ipdb/__init__.py", line 7, in <module>
from ipdb.__main__ import set_trace, post_mortem, pm, run # noqa
File "/usr/local/lib/python2.7/site-packages/ipdb/__main__.py", line 51, in <module>
ipapp.initialize([])
File "<decorator-gen-109>", line 2, in initialize
File "/usr/local/lib/python2.7/site-packages/traitlets/config/application.py", line 74, in catch_config_error
return method(app, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/IPython/terminal/ipapp.py", line 315, in initialize
self.init_code()
File "/usr/local/lib/python2.7/site-packages/IPython/core/shellapp.py", line 263, in init_code
self._run_startup_files()
File "/usr/local/lib/python2.7/site-packages/IPython/core/shellapp.py", line 342, in _run_startup_files
self._exec_file(python_startup)
File "/usr/local/lib/python2.7/site-packages/IPython/core/shellapp.py", line 328, in _exec_file
raise_exceptions=True)
File "/usr/local/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2469, in safe_execfile
self.compile if kw['shell_futures'] else None)
File "/usr/local/lib/python2.7/site-packages/IPython/utils/py3compat.py", line 288, in execfile
builtin_mod.execfile(filename, *where)
File "~/.pythonrc", line 57, in <module>
import traceback; traceback.print_stack()

You can ask the traceback module:
$ cat .pythonrc
import traceback; traceback.print_stack()
$ cat test.py
import ipdb
Then by running
$ PYTHONSTARTUP="$HOME/.pythonrc" python test.py
you should get a traceback that tells you exactly from where the startup script is being run. Most likely, this is due to a call
start_ipython()
somewhere in the ipdb import.

Related

"python -m idlelib.idle" not working - Windows 10

I'm running Python 3.10.2288.0 (x64) on a Windows 10 OS.
I use IDLE as interpreter.
So far, I have been running IDLE (in and out venv) by:
C:path\to\venv venv\Scripts\Activate
(venv) C:path\to\venv python -m idlelib.idle
All of a sudden, this command didn't work anymore.
The following error appeared:
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\idlelib\idle.py", line 13, in <module>
from idlelib.pyshell import main # This is subject to change
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\idlelib\pyshell.py", line 53, in <module>
from idlelib import debugger
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\idlelib\debugger.py", line 7, in <module>
from idlelib import macosx
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\idlelib\macosx.py", line 7, in <module>
from test.support import requires, ResourceDenied
ModuleNotFoundError: No module named 'test'
I tried to reinstall Python, but nothing changed.
The same error appears when running command outside the venv.
It seems that everything is up-to-date.
What do you suggest?
Thanks,
idromv
This is the result of a buggy fix to a bug that is triggered when Python is installed without the test module. The bug appeared in 3.10.8 and 3.12.0a1, released a week ago, and at least in Microsoft Store Python 3.10.2288.0.
Omitting the test module seem to not currently be an option on Mac, so this should not be a problem there.
One fix anywhere is to edit /Lib/idlelib/macosx.py: delete line 7, from test.support import requires, ResourceDenied, and replace line 23, requires('gui') with pass. This may require admin privileges.
On Windows, rerun the python.org installer, chose Modify, and check the box to install the test suite.
For the Windows store Python, including the test suite may not be an option. One can, as mentioned in the comments, instead get the python.org installer and install with the test suite.
I don't know what the situation is for *nix.

Can not run robotframework-ride properly

I have installed robotframework-ride on two PCs of mine. Everything works fine on one PC but could not have RIDE running on other PC.
I have the following installed on both PCs
- Python 2.7.8 on win32
- Robot framework 2.8.6
- wxPython 2.8.12.1
- robotframeowkr-ride-1.4.1b
When I tried to execute "python ride.py" on the second PC, I got a message:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\robotide__init__.py", line 74, in main
run(inpath, not noupdatecheck, debug_console)
File "C:\Python27\lib\site-packages\robotide__init_.py", line 100, in run
ride = RIDE(inpath, updatecheck)
File "C:\Python27\lib\site-packages\robotide\application\application.py", line
41, in __init_
wx.App.init(self, redirect=False)
File "C:\Python27\lib\site-packages\wx-2.8-msw-unicode\wx_core.py", line 7981
, in init
self.BootstrapApp()
File "C:\Python27\lib\site-packages\wx-2.8-msw-unicode\wx_core.py", line 7555
, in _BootstrapApp
return _core.PyApp__BootstrapApp(args, *kwargs)
File "C:\Python27\lib\site-packages\robotide\application\application.py", line
49, in OnInit
self.namespace = Namespace(self.settings)
File "C:\Python27\lib\site-packages\robotide\namespace\namespace.py", line 41,
in init
self._set_pythonpath()
File "C:\Python27\lib\site-packages\robotide\namespace\namespace.py", line 62,
in _set_pythonpath
self._settings.get('installed robot version', None))
File "C:\Python27\lib\site-packages\robotide\namespace\robotlibraryloader.py",
line 35, in find_installed_robot_libraries
rf_file, rf_version = output.strip().split(', ')
ValueError: too many values to unpack
Use --help to get usage information.
Something more interesting is, if I uninstall robot framework from the second PC, I could execute "python ride.py" but of course there is no point doing this.
Anyone can help me out here?
With kind assistance from janne, I have solved this problem. The cause is for the first PC I installed the correct "robotframework", but for the second PC I managed to install a wrong module, "robot" instead of "robotframework". That was why RIDE was not working.
I am sorry for making such a silly mistake. A big thank you to janne for his help.
should use: pip install robotframework
should NOT use: pip install robot

Launching IPython qtconsole from command line (after installing all dependencies)

I am a bit of a newbie to the command line. I used to have IPython (with all dependencies configured) on my last MacBook, such that I could click on an icon from the dash to launch iPython qtconsole (outside of terminal shell).
Now, on my new MacBook Pro, after installing all of the same files and dependencies, I am getting:
Jacobs-MacBook-Pro:~ Jacob$ ipython qtconsole
Traceback (most recent call last):
File "/usr/local/bin/ipython", line 8, in <module>
load_entry_point('ipython==1.0.dev', 'console_scripts', 'ipython')()
File "/Library/Python/2.7/site-packages/ipython-1.0.dev-py2.7.egg/IPython/frontend/terminal/ipapp.py", line 390, in launch_new_instance
app.initialize()
File "<string>", line 2, in initialize
File "/Library/Python/2.7/site-packages/ipython-1.0.dev-py2.7.egg/IPython/config/application.py", line 84, in catch_config_error
return method(app, *args, **kwargs)
File "/Library/Python/2.7/site-packages/ipython-1.0.dev-py2.7.egg/IPython/frontend/terminal/ipapp.py", line 315, in initialize
super(TerminalIPythonApp, self).initialize(argv)
File "<string>", line 2, in initialize
File "/Library/Python/2.7/site-packages/ipython-1.0.dev-py2.7.egg/IPython/config/application.py", line 84, in catch_config_error
return method(app, *args, **kwargs)
File "/Library/Python/2.7/site-packages/ipython-1.0.dev-py2.7.egg/IPython/core/application.py", line 323, in initialize
self.parse_command_line(argv)
File "/Library/Python/2.7/site-packages/ipython-1.0.dev-py2.7.egg/IPython/frontend/terminal/ipapp.py", line 310, in parse_command_line
return super(TerminalIPythonApp, self).parse_command_line(argv)
File "<string>", line 2, in parse_command_line
File "/Library/Python/2.7/site-packages/ipython-1.0.dev-py2.7.egg/IPython/config/application.py", line 84, in catch_config_error
return method(app, *args, **kwargs)
File "/Library/Python/2.7/site-packages/ipython-1.0.dev-py2.7.egg/IPython/config/application.py", line 429, in parse_command_line
return self.initialize_subcommand(subc, subargv)
File "<string>", line 2, in initialize_subcommand
File "/Library/Python/2.7/site-packages/ipython-1.0.dev-py2.7.egg/IPython/config/application.py", line 84, in catch_config_error
return method(app, *args, **kwargs)
File "/Library/Python/2.7/site-packages/ipython-1.0.dev-py2.7.egg/IPython/config/application.py", line 361, in initialize_subcommand
subapp = import_item(subapp)
File "/Library/Python/2.7/site-packages/ipython-1.0.dev-py2.7.egg/IPython/utils/importstring.py", line 40, in import_item
module = __import__(package,fromlist=[obj])
File "/Library/Python/2.7/site-packages/ipython-1.0.dev-py2.7.egg/IPython/frontend/qt/console/qtconsoleapp.py", line 56, in <module>
from IPython.external.qt import QtCore, QtGui
File "/Library/Python/2.7/site-packages/ipython-1.0.dev-py2.7.egg/IPython/external/qt.py", line 23, in <module>
QtCore, QtGui, QtSvg, QT_API = load_qt(api_opts)
File "/Library/Python/2.7/site-packages/ipython-1.0.dev-py2.7.egg/IPython/external/qt_loaders.py", line 241, in load_qt
result = loaders[api]()
File "/Library/Python/2.7/site-packages/ipython-1.0.dev-py2.7.egg/IPython/external/qt_loaders.py", line 171, in import_pyqt4
from PyQt4 import QtGui, QtCore, QtSvg
ImportError: dlopen(/Library/Python/2.7/site-packages/PyQt4/QtGui.so, 2): Library not loaded: /usr/local/lib/QtGui.framework/Versions/4/QtGui
Referenced from: /Library/Python/2.7/site-packages/PyQt4/QtGui.so
Reason: image not found
and, again being a newbie, I think part of the problem may rely on the output, here:
Jacobs-MacBook-Pro:~ Jacob$ brew install qt
Warning: It appears you have MacPorts or Fink installed.
Software installed with other package managers causes known problems for
Homebrew. If a formula fails to build, uninstall MacPorts/Fink and try again.
Warning: qt-4.8.4 already installed, it's just not linked
Thanks in an advance for any tips. And if you need me to run any command (to view more outputs) just let me know!
So, you have multiple Python installations, and aren't sure which one you have.
You have both Homebrew and MacPorts.
Your MacPorts is broken and you don't know how to fix it.
Fixing each of these may not be that hard, but I think it's time to wipe the slate clean and start over.
The "easy" way to do this is to reinstall the OS, using the standard backup-and-migrate stuff to preserve your Aqua-level apps, user preferences, documents, etc. But that's pretty drastic, and shouldn't be necessary.
To clean things up manually, first:
Uninstall MacPorts.
brew uninstall $(brew list). This removes all Homebrew packages. You could just unlink instead of uninstall, but you really want to rebuild them once your machine is cleaned up.
sudo rm -rf /Library/Python /Library/Frameworks/Python.framework /usr/local/share/python*. This wipes out most third-party Python installations. And if you don't know where yours came from, it's really the best you can do.
Take a look at whatever's left in /usr/local/bin. All kinds of stuff can end up here, from scripts installed with Apple's Python to the command-line tools for GUI apps like TextMate, Aquamacs, or GitHub to the tools that come with binary installs of SDL or Qt, so you may not want to just wipe out the whole thing—but you do want to look it all over. Also look at /usr/local/lib and /Library/Frameworks.
You also may need to edit your ~/.profile, etc. files to undo changes you or those installers may have made, like adding /opt/local/bin or /Library/Frameworks/Python.framework/Versions/2.7/bin to your PATH. There could also be changes in /etc, but that's less likely (and less likely to cause problems).
Now run brew doctor to make sure Homebrew is happy, reinstall any non-Python-related Homebrew packages and binary installers you want, and now you're ready to install/configure your python, PyQt4-or-PySide, iPython, etc. properly and live happily every after (until you buy a new computer next year).

Twisted under windows

Twisted located at C:\Python26\Lib\site-packages\Twisted-11.0.0-py2.6-win32.egg\twisted
but
C:\projects\webmorda>twistd -n morda_server
Traceback (most recent call last):
File "C:\Python26\Scripts\twistd.py", line 4, in <module>
import pkg_resources
File "C:\Python27\lib\site-packages\pkg_resources.py", line 2671, in <module>
working_set.require(__requires__)
File "C:\Python27\lib\site-packages\pkg_resources.py", line 654, in require
needed = self.resolve(parse_requirements(requirements))
File "C:\Python27\lib\site-packages\pkg_resources.py", line 552, in resolve
raise DistributionNotFound(req)
pkg_resources.DistributionNotFound: twisted==11.0.0
what's wrong?
It looks like you have installed Twisted in a Python 2.6 environment but are using Python 2.7 to run it. I think the following command should work:
C:\Python26\python.exe C:\Python26\Scripts\twistd.py -n morda_server
(It looks like the twistd.py script is being run by the python.exe binary associated with the .py file extension - which in your case appears to be Python 2.7. Alternatively you have a PYTHONPATH environment variable set up to point to the Python 2.7 site-packages directory.)

Problem running python from crontab - "invalid Python installation"

I have python 2.7 installed on my linux box, and I'm trying to schedule a python script via crontab. The script works fine from the command line, however when running via cron I get:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site.py", line 553, in <module>
main()
File "/usr/local/lib/python2.7/site.py", line 535, in main
known_paths = addusersitepackages(known_paths)
File "/usr/local/lib/python2.7/site.py", line 268, in addusersitepackages
user_site = getusersitepackages()
File "/usr/local/lib/python2.7/site.py", line 243, in getusersitepackages
user_base = getuserbase() # this will also set USER_BASE
File "/usr/local/lib/python2.7/site.py", line 233, in getuserbase
USER_BASE = get_config_var('userbase')
File "/usr/local/lib/python2.7/sysconfig.py", line 535, in get_config_var
return get_config_vars().get(name)
File "/usr/local/lib/python2.7/sysconfig.py", line 434, in get_config_vars
_init_posix(_CONFIG_VARS)
File "/usr/local/lib/python2.7/sysconfig.py", line 298, in _init_posix
raise IOError(msg)
IOError: invalid Python installation: unable to open /usr/include/python2.7/pyconfig.h (No such file or directory)
I see that /usr/include/python2.7 does't exist, but /usr/local/include/python2.7/ does. Did I make a mistake while installing python?
You probably just have 2 versions installed, one of which is broken. If your cron is just directly calling python instead of a specific path, your PATH probably contains /usr/bin before /usr/local/bin (which is typical) - so in your cron, specify which python to use, or remove the existing one in /usr/bin and symlink /path/to/good/python to /usr/bin/python.
Edit: scratch that, just re-read and saw that it works fine from the command line. python-dev is probably the way to go. Sorry!
You need python2.7-dev, which installs the includes and headers.
For Ubuntu, you run sudo apt-get install python2.7-dev to install it. What Linux distro are you running?
I am assuming that in your crontab file, you are giving the full path to the python executable, and not just relying on she-bang with executable permissions. If not, please give point to the full-path python2.7 in the crontab file and also use the same full-path on the command line to ensure that you don't get this problem.If you get this on command line too, then it is probably missing some development headers. (Are you trying to compile something like using setup.py build and trying to do it via crontab?) I am trying to understand where would one need those headers. So, apart from the above suggestion, extra information from your end might ofcourse help further.

Categories

Resources