I am trying to run TCL scripts from Python. I have a third-party TCL package included in the TCL script (which I have to use) which makes calls to "console". Because of this, if I just run the following:
z = x.tk.eval('source C:/somePath/GetStatsFirst2.tcl')
I get the following error:
pydev debugger: starting
WARNING!!! Unable to add paths from Appinfo: Could not find AppInfo registry entry
WARNING!!! Unable to add paths from Appinfo: Could not find AppInfo registry entry
Traceback (most recent call last):
File "C:\Users\lab\Documents\Public\eclipse\plugins\org.python.pydev_2.7.5.2013052819\pysrc\pydevd.py", line 1397, in <module>
debugger.run(setup['file'], None, None)
File "C:\Users\lab\Documents\Public\eclipse\plugins\org.python.pydev_2.7.5.2013052819\pysrc\pydevd.py", line 1090, in run
pydev_imports.execfile(file, globals, locals) #execute the script
File "C:\Users\lab\Documents\Public\workspace\Version 1\....\TC1.py", line 55, in <module>
test()
File "C:\Users\lab\Documents\Public\workspace\Version 1\....\TC1.py", line 42, in test
z = x.tk.eval('source C:/Users/lab/Documents/Public/TCL/Scripts/GetStatsFirst2.tcl')
_tkinter.TclError: invalid command name "console"
This definitely has something to do with the package I am using and the problem may be unavoidable. Since there is no TK console that is opened (because I am using the TK inter class and eval), I get the feeling there is a way around this. It looks to me like the package I am importing requires the existence of a TK console. When run on the command line, or through subprocess.call, everything works, but in those cases a console is opened. I am pretty sure the package is actually looking for a console. Is there a way to create the console along with the Tk object?
Ahh, the console command.
The console command is only available
on Windows
only with wish
and only in the master interp.
I suggest that you use tkcon instead. You just need to find the tkcon.tcl file, source it before you source GetStatsFirst2.tcl and execute the following Tcl command:
interp alias {} console {} tkcon
This uses tkcon as console then.
Edit: You can execute that Tcl command with
x.tk.eval('interp alias {} console {} tkcon')
in Python.
Related
I am new to python and I found a very intesting library I need to play with which is only written in python
here us part of my code
from pkgutil import get_data
grammar = get_data(__name__, "thl.lark").decode("utf-8")
Now when I run it I get
Traceback (most recent call last):
File "/Users/xxx/thl/thl-jupyter-2020/thl/test.py", line 6, in <module>
grammar = get_data(__name__, "thl.lark").decode("utf-8")
File "/xxx/.pyenv/versions/3.9.0/lib/python3.9/pkgutil.py", line 620, in get_data
spec = importlib.util.find_spec(package)
File "/xxx/.pyenv/versions/3.9.0/lib/python3.9/importlib/util.py", line 114, in find_spec
raise ValueError('{}.__spec__ is None'.format(name))
ValueError: __main__.__spec__ is None
Any idea what is going on?
If you run your script with -m flag provided it will work.
It means that your script will be executed as a module rather than a standalone script. I can assume that pkgutil requires a module to work.
python -m your_script.py
There's a great blog post about python modules and the difference between standalone scripts.
Start with solving line 6 and peel off parts you are unsure of, then adding them piece by piece.
pkgutil.get_data is a function for retrieving resource files from packages.
Your test.py is not a package. It makes no sense to try to use pkgutil.get_data with it, as your file is an ordinary module, not a package, and it cannot contain any resource files.
When I click on my Python IDE's (IDEL, PyScripter) they will not even open. I tried typing python in the command prompt and this is what happened:
C:\>python
Traceback (most recent call last):
File "C:\Python27\ArcGIS10.5\lib\site.py", line 548, in <module>
main()
File "C:\Python27\ArcGIS10.5\lib\site.py", line 537, in main
aliasmbcs()
File "C:\Python27\ArcGIS10.5\lib\site.py", line 469, in aliasmbcs
codecs.lookup(enc)
File "C:\Python27\ArcGIS10.5\lib\encodings\__init__.py", line 85, in search_function
norm_encoding = normalize_encoding(encoding)
File "C:\Python27\ArcGIS10.5\lib\encodings\__init__.py", line 57, in normalize_encoding`enter code here`
encoding = str(encoding, "ascii")
TypeError: str() takes at most 1 argument (2 given)
Did you recently install ArcGIS? It looks to me like ArcGIS installed a few libraries, and overwrote your site.py, but it's using code that's meant for Python3 rather than Python2.7. The str function is capable of taking in 2 arguments in Python3 but not in Python2.
To get your Python to work again, you could try deleting the entire ArcGIS10.5 directory from your computer (or temporarily moving it to your desktop and seeing if that helps). You can also try running python -S in Command Prompt to run Python without importing site.py.
To try to get ArcGIS working, you might be able to install Python3, and reinstall ArcGIS using that.
Hopefully that helps!
I am trying to have a Python script automatically download an updated version of itself, replace the existing version, then restart automatically so that it loads the new version.
I'm currently using the following code to restart it:
os.execv(__file__, sys.argv)
However, this isn't working. Whenever Python tries to run this line, it returns the following error:
Traceback (most recent call last):
File "N:\CardDB\Station\Read.py", line 195, in <module>
else:
File "N:\CardDB\Station\Read.py", line 187, in run_update
print("\n\nWould you like to install this update?")
File "N:\CardDB\Station\Read.py", line 144, in update
f.write(version)
OSError: [Errno 8] Exec format error
Other questions on StackOverflow suggest that it's due to a missing shebang line, but I've made sure that it isn't missing - the first line of my script is:
#!/usr/bin/env python3
Unlike unix OSes, Windows does not seem to have native support for interpreted executables. os.execv requires a binary and fails because it is given a text file. So instead of calling the script directly, call the python interpreter
os.execv(sys.executable, [sys.executable, __file__] + sys.argv)
On, e.g., Linux, you can use os.execv(__file__, [__file__] + sys.argv) if the script is marked executable and contains a shebang line. For nonexecutable scripts you have to call the python executable as above.
I'm new to Python and I'm having some problems trying to make PLY works. For now, all I want is to successfully run the example from the PLY homepage.
At first I tried to just download PLY-3.8, put the ply folder in the same directory I saved the example (calc.py) and ran it. The calc.py file is at the C:\Users\...\Python directory and the ply folder is the C:\Users\...\Python\ply, just to make it clearer. But I got an ImportError: No module named 'ply'.
Then I searched for a while, tried to update something called distutils and install the modules through the Windows PowerShell and so on and so forth, but none of that worked and I just reset the whole thing (reinstalling Python and all of that). But then I finally got it to work by simply inserting into the sys.path the directory path where the script I was running (edit: in interactive mode) was, by doing this:
import sys
sys.path.insert(0,'C:\\Users\\ ... \\Python')
This fixed the ImportError but, and this is where I am now, there are a bunch of other errors:
Traceback (most recent call last):
File "C:\Users\...\Python\calc.py", line 48, in <module>
lexer = lex.lex()
File "C:\Users\...\Python\ply\lex.py", line 906, in lex
if linfo.validate_all():
File "C:\Users\...\Python\ply\lex.py", line 580, in validate_all
self.validate_rules()
File "C:\Users\...\Python\ply\lex.py", line 822, in validate_rules
self.validate_module(module)
File "C:\Users\...\Python\ply\lex.py", line 833, in validate_module
lines, linen = inspect.getsourcelines(module)
File "c:\users\...\python\python35\lib\inspect.py", line 930, in getsourcelines
lines, lnum = findsource(object)
File "c:\users\...\python\python35\lib\inspect.py", line 743, in findsource
file = getsourcefile(object)
File "c:\users\...\python\python35\lib\inspect.py", line 659, in getsourcefile
filename = getfile(object)
File "c:\users\...\python\python35\lib\inspect.py", line 606, in getfile
raise TypeError('{!r} is a built-in module'.format(object))
TypeError: <module '__main__'> is a built-in module
Now I have absolutely no idea what to do. I tried to search for a solution but had no luck. I appreciate if anyone can help me out.
I'm on Windows 10, using Python 3.5.0 and iep as my IDE (www.iep-project.org) if these informations are of any importance.
In short: I just want to successfully run the example from the PLY homepage and then I think I can figure out the rest.
EDIT: I found out that if I do:
import inspect
inspect.getfile(__main__)
I get the exact same (last) error from before:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "c:\users\...\python\python35\lib\inspect.py", line 606, in getfile
raise TypeError('{!r} is a built-in module'.format(object))
TypeError: <module '__main__'> is a built-in module
I think this is the culprit, but I still don't know how to fix it.
EDIT 2: I got it to work and answered the question explaining how, but if someone have a more complete answer, I would love to hear it.
To anyone having this problem, I found what was the issue. I still don't know why exactly is like that, so if anyone have a more complete answer to provide I would appreciate (I'm still a newbie at Python).
Anyway, it seems this code can't be executed in Interactive mode, it needs to be executed as a script. To do that on IEP it's Run > Run file as script or Ctrl+Shift+E. On IDLE you need to Open... the file (Ctrl+O) and then Run Module (F5).
As to why it can't be executed in Interactive mode, here's a little bit about the difference between interactive mode and running as script from the IEP wizard:
Interactive mode vs running as script
You can run the current file or the main file normally, or as a script. When run as script, the shell is restared (sic) to provide a clean environment. The shell is also initialized differently so that it closely resembles a normal script execution.
In interactive mode, sys.path[0] is an empty string (i.e. the current dir), and sys.argv is set to [''].
In script mode, __file__ and sys.argv[0] are set to the scripts filename, sys.path[0] and the working dir are set to the directory containing the script.
That explains a bit about why the inspect.getfile(__main__) was throwing an error: the __main__ had no attribute __file__. And also why I had to insert the current directory into sys.path: sys.path didn't had the current directory in interactive mode.
I hope this helps someone.
So i'm trying to setup the python code checkers suggested in the emacs wiki. However, I'm unable to run those scripts in my command shell let alone emacs.
The section is found here:
http://www.emacswiki.org/emacs/PythonProgrammingInEmacs#toc7
And I tried the script located here and here
In both cases I changed the first line from #!usr/bin python with the full path of my python executable and when I run the scripts via
python pylint_etc_wrappers.py someModule.py
or
python pycheckers.py soemModule.py
both boil down to the same error, most likely because they try to open a subprocess. Here's the trace:
Traceback (most recent call last):
File "pycheckers.py", line 254, in <module>
runner.run(source_file)
File "pycheckers.py", line 91, in run
process = Popen(args, stdout=PIPE, stderr=PIPE)
File "C:\devel\Python\Python-2.7\Lib\subprocess.py", line 672, in __init__
errread, errwrite)
File "C:\devel\Python\Python-2.7\Lib\subprocess.py", line 882, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
The second script suggests to change the first line to the path of the interpreter (which I did) and to change the path in the main function which looks something like :
os.environ['PATH'] = \
path.dirname(sys.executable) + ':' + os.environ['PATH']
which was a bit unclear to me. Any ideas?
I have pylint 0.25.1, installed using easy_install (Python 2.7, Win XP). Both pylint and pylint.bat were installed in Python27/Scripts (this directory is in my PATH).
I too get the "The system cannot find the file specified" error when running the pylint_etc_wrapper.py script unchanged.
Running pylint from the script does work if
command = 'pylint'
is changed to
command = 'pylint.bat'
Another way to make it work is to add shell=True to the Popen() call.
I can't really explain all this, but there is an unresolved Python bug that looks like it might be relevant: http://bugs.python.org/issue8557.