Ok I know this has been asked/answered a bunch of times, but the solutions are not working in my situation. I created a Python script using 2.7 on a headless linux terminal on a Raspberry Pi, it works perfectly in that environment.
The script contains an sqlite3 database. On my Windows 10 machine, if I simply double click the Python script from Desktop it creates the database.db file, but the Python command prompt closes before I can see the output.
Running the Python script from the Windows Command prompt eg 'start C:\Users\User\Desktop\script.py' does exactly the same thing
Using Run and 'cmd /k C:\Users\User\Desktop\script.py' does make the Command window persist, but then it errors on the line opening the sqlite3 database:
Traceback (most recent call last):
File "C:\Users\User\Desktop\script.py", line 91, in <module>
conn = sqlite3.connect("database.db")
sqlite3.OperationalError: unable to open database file
The reason Im on Windows at all is Im trying to build a GUI with Tkinter and my R Pi has no GUI, help appreciated
You need to pass the full path like C:\Users\User\Desktop\database.db.
The reason is when running from 'cmd /k C:\Users\User\Desktop\script.py' then the working directory where the command is called is different and it tries to look database.db in that folder, and gives you the error.
You can check what the present dir is by :
import os
print os.getcwd()
Related
I had this error when I trying to use example from CS50 introduction in AI course
I use win 10 in my end , they using MAC its worked in their end but didn't work in my end
what is the issue or what I need to do to run this txt file with my code using the windows terminal
You are running maze.py from "C:>"
Open file explorer.
Navigate to your Desktop
In the top type cmd
Pess Enter
In cmd run
maze.py maze3.txt
Make sure the maze3.txt file is in you Desktop, from where you're running the script. From the error, it seems like that file is not there.
I have a technical questions on python script.
I have developed code on my laptop and I want to move it in another one.
I installed python there, put when I try to double click on the .py file it is not working. Like the cmd screen goes away in a second.
Do you know why?
(if I open python through cmd it is working, so it is in path and it works)
What is probably happening is that there is an error being thrown on your new computer which causes command prompt to just instantly close.
The best way to run a python script is from an open command prompt/terminal. To do this open a command prompt and move into the directory of your python file. For example, if the file you are trying to run is located at C:\Users\Davide\PythonScripts, then open a command prompt and type
cd C:\Users\Davide\PythonScripts
Now your command window is in the folder that you want to run files out of.
Next you want to tell Python to run your script. This can be done by typing "python ". For example, if your script is name my_script.py, you would type
python my_script.py
What this is doing is telling your computer "open an instance of Python where you are running my_script.py." If there are any errors thrown, the command window will stay open after python closes and you can see what is going on.
Most likely, there is a package you are trying to import which it cannot find because it was not installed on your other computer. If at the top of your file you have "import xxx" or "from xxx import yyy" lines, your other computer might not be finding those modules and just throwing an error that instantly closes command prompt when you just double click the .py file.
I am new to Python and currently doing a basic python course to learn. I have been running code all day via the Command Prompt and it has been working fine. For some reason though it has stopped working and python files I try to run are returning the following error:
Traceback (most recent call last):
File "", line 1, in
NameError: name 'hello' is not defined
As per the screen shot and the numbers on it, I performed the following steps in an attempt to run the file:
change to the folder where file is saved
run 'dir' to list all files. I am trying to run 'hello.py'. This contains the code: print('hello simon!')
I can run the file by just typing hello.py into the command prompt, this works ok
I can also run with: python hello.py - this works ok
when I activate Python by typing Python --> Enter, this starts the interpreter ok. However if I then try to run by typing hello.py I get the error message.
This has worked ok all day, I have not changed anything on my PC (to the best of my knowledge!) but just started to get this error a few hours ago. I have looked all over the internet for solution but found nothing. I have uninstalled and re-installed Python, restarted etc... all to no avail.
I am running Python 3.6.5 on a Windows 7 64 bit PC.
It won't let me attach a picture so here is link to screenshot of Command Prompt and error: https://i.stack.imgur.com/BBUe5.jpg
I hope someone can help me with this please
Thankyou
You are not supposed to execute hello.py in the Python Interpreter. It won't work. When you type in python and hit Enter in your Command Prompt, Just type this,
>>> print('hello simon!')
And hit Enter, it would definitely work. Because the interpreter is supposed to execute a code line by line. So if you want to run a Python Script then do not execute it in the Interpreter.
The problem is that when you write python (alone), the command line calls python shell and the following commands are run inside the python shell and not in the command line anymore. Calling a script from the shell has a different format (look it up). You can enter exit() to exit the shell back to command line again
What you are trying to achieve is you are running Hello.py inside Python.
Not with Python.
You need to run Hello.py with Python. As python is interpreter over here.
>>>python
means you are going inside python shell
>>>print('hello simon!')
Is equivalent to your program.
You are running your Python Script as you should and it's working. If you added Python to your path you can run Script you only need to call the Script "hello.py". If you have more than one intepreter, or you didn't added it to your path then you can call it like this "C:\path\to\python\interpretet\pythonxxx.exe" "c:\path\to\python\script.py" you can enven pass arguments to it "C:\path\to\python\interpretet\pythonxxx.exe" "c:\path\to\python\script.py" --argument
When you type python in a shell, then interactive mode is activated. This is like a shell where you type commands and got interpreted right away, in the same way as cmd and powershell works, but for python. This way you can test snippets, or just do simple stuff overly complicated like this
import os
ls = os.listdir(os.path.abspath('c:/'))
def print_dir():
for file in ls:
print(file)
Wich in cmd would be dir c:\ or in powershell ls c:\. The point is that you can test libraries, explore objects, replace the shell or just have fun.
I am new to python.
I am trying to run my first script... I think this is what is called.
I have following in a python doc called "intro.py":
print('hello world')
This is saved under my Desktop (running Windows).
When I go to cmd, I type:
>>> Desktop/intro.py
When I do this I get a response that says:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Desktop' is not defined
Can someone please explain to me my issue? Thanks
Windows shell refers to what you see, when you hit windows-button + r and then type cmd and hit enter.
To start your script: hit windows-button + r and then type cmd and hit enter
After that navigate in the command line to your desktop using cd and then type python init.py to execute your script.
It seems to me like you were already in the python shell, when you tried to start your script.
To see the difference open the command line like before: hit windows-button + r and then type cmd and hit enter, remember how your terminal looks like now, then type in python and note the three >>> at the beginning of the line. This is then called the Python shell.
You tried to execute windows commands in this python shell, which obviously does not work. However you could also theoretically write your script in this python shell. But this is really awkward.
I suggest you to use an IDE. If I'm not wrong IDLE comes with the python 3 interpreter already or I can recommend my personal favorite PyCharm.
If I call a script file from Python interpreter or a Python script, I get the error 'No such file or directory' and the Linux script does not execute.
If I call pure Linux commands, like subprocess.call('ls') it works fine.
In 'C', I would use: system("");
What is the equivalent in Python?
From my Python script, on a Linux computer, I need to call scripts that do builds.
subprocess.call works great for Linux shell commands, but I get 'No such file or directory' when I call a script.
Here's my Python script line that calls:
rtn_code = subprocess.call(['~/WindRiver/workspace/XPedite5570_SMP_VIP/build_MM_VxWorks'])
`build_MM_VxWorks` is a Linux script that uses VxWorks development tools to build a VxWorks kernel.
`build_MM_VxWorks` works okay from the Linux command line.
You might want to try out pexpect . It simulates shell user-input on a high level. It might be a bit "hacky", but it is very powerful when you need to automate working procedure.
Here is how I use it to read a table in a database:
import pexpect, getpass
child = pexpect.spawn('mysql -u root -p')
child.expect('Enter password: ')
password = getpass.getpass("Enter Mysql password for user root \n")
child.sendline(password)
child.sendline("use database")
child.expect('Database changed')
child.sendline('SELECT * FROM table;')
child.interact()
As you see, it is very easy to mimic user input.