I have a python file and I want to execute it from PyCharm console.
I'm in the directory of the project (where there is my file)
I tried to follow the solution in this thread: Running a module from the pycharm console
but I got the following error:
from project_main_folder import *
home_scorer_macro.py
Traceback (most recent call last):
File "C:\Python35\lib\site-packages\IPython\core\interactiveshell.py", line 2963, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-20-24aaf6f313c6>", line 1, in <module>
home_scorer_macro.py
NameError: name 'home_scorer_macro' is not defined
I tried directly without "import" to call execute the file, like:
python home_scorer_macro.py
but I got the following error as well:
File "<ipython-input-29-735c29c27d57>", line 1
python home_scorer_macro.py
^
SyntaxError: invalid syntax
You are confusing the console for the command prompt. The console allows you to type code and have it interpreted as you type line by line (or function by function). The command prompt allows you to say things like python home_scorer_macro.py. If you are trying to run the home_scorer_macro function from learning you can try:
from learning import home_scorer_macro
home_scorer_macro()
It is difficult to tell what you are trying to do without you providing any description of what you are trying to accomplish, though. You could also try something like
import os
os.system('"python home_scorer_macro.py"')
raw_input()
if you would like to combine commmand prompts and consoles. The os.system lets you run commands from within your python script
As well you can go to the Terminal tab and run python home_scorer_macro.py
Related
Something in my setup of my shell causes arbitrary strings like "krmpfl" or "u45g5svtJ7" to create a Python error:
$> krmpfl
Traceback (most recent call last):
File "/usr/lib/command-not-found", line 28, in <module>
from CommandNotFound import CommandNotFound
File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 19, in <module>
from CommandNotFound.db.db import SqliteDatabase
File "/usr/lib/python3/dist-packages/CommandNotFound/db/db.py", line 5, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
I would expect bash (and not python!) to throw an error of the kind "Unknown command krmpfl. Did you mean...", but any non-recognized command is for some reason passed to python. I am confused.
Does anyone have an idea on how to debug this or how to move forward? I've tried type krmpfl but this (correctly) echoes bash: type: krmpfl: not found
My setup:
Win10 using Ubuntu 18.04 within WSL
ConEmu as a console
Bash-it
Python 3.8
Click (python package) installed to simplify creating commands
If your current shell function defines a function named command_not_found_handle, bash runs that for a non-existent command rather than immediately failing with a "command not found" error. In your case, that function exists and calls /usr/lib/command-not-found, which appears to be a Python script that tries to download (or at least suggest you download) a package with apt_pkg, but you don't have that Python module installed, which leads to the Python exception.
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 am a beginner in Python and am learning via online tutorials. On Study Drill #6 for example 15 on this page, I'm trying to open up the 'ex15_sample.txt' file that I have created and exists.
This is what it says to do:
Start 'python' to start the Python shell, and use 'open' from the prompt just like in this program. Notice how you can open files and run 'read' on them from within 'python'?
In the command prompt, I entered python to start the Python shell and have tried typing in the following, which threw me an error:
open(ex15_sample.txt)
The error I get is:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'ex15_sample' is not defined
ex15_sample.txt is a file that exists in the same folder. How am I supposed to use open or any other command from the prompt? I am running Python 2.7.8 on Powershell, by the way.
The first argument of open is a string containing the filename.
Just put file name in Quotation Mark:
f = open('ex15_sample.txt')
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.