I've recently installed Python 3.3 in addition Python 2.7 on my Computer (Windows 7, 32-bit). Python 3.3 gives you the possibility of adding a "shebang line" at top of your .py files so when you execute them it can select which Python version to use. Since Pyscripter doesn't recognise this "shebang lines", i wrote a programm which reads the first line of a .py file and then opens it in Pyscripter with the correspondent Argument.
It looks like this:
#!/usr/bin/env python2.7
from sys import argv
from os import system
if len(argv)>1:
file = open(argv[1])
shebang=file.readline()
if shebang.split()[1] in {'python2','python2.7'}:
system(r'C:\Python27\PyScripter.exe --python27 "'+argv[1]+'"')
elif shebang.split()[1] in {'python3','python3.3'}:
system(r'C:\Python33\PyScripter.exe --python33 "'+argv[1]+'"')
else:
system(r'C:\Python27\PyScripter.exe --python27')
file.close()
exit()
I then compiled the program using py2exe and selected is as standard operation for .py files. When I open a .py file now, PyScripter opens the file using the correct version of python, but when i try to save the file it says:
Error saving file: "C:\Users\...\Python\example.py".
Error: Cannot create file "C:\Users\...\Python\example.py". The process cannot acces the file because it is being used by another process
The compiled program for opening PyScripter is still running, but even if i kill it it still brings that message. Same thing happens with a cmd.exe window opened by the program. If i try to delete/rename/move the file in Windows Explorer it say i can't do it, because the file is currently being used by PyScripter.exe.
Does anybody know how to solve this?
The program to open PyScripter is Python 2.7, since i don#t have py2exe for Python 3.
Related
I created a script in python and I need it to run in way that the python code is not visible. I tried different methods like implementing nodeJS and PHP, but to no avail. So, I thought of using a .bat file to run the python file. I created a bat file for the python script, but on clicking the batch file all I got was an error saying that a module was missing in the code. The code works perfectly when I run it in Spyder and on the Anaconda Prompt. So, I thought of running the file from a batch file via the Anaconda Prompt. The code in the batch file is below:
%windir%\System32\cmd.exe /K ""C:\Program Files\Anaconda3\Scripts\activate.bat" "C:\Program Files\Anaconda3""
python myscript.py
pause
The .bat file now opens a command prompt in the python environment, but does not run the python file. I tried altering the code, but it was always the same result. If I type:
python myscript.py
into the next line in the command prompt, the python file gets executed. I need to find a way for the file to be executed without typing anything in the prompt. The python file contains GUI from tkinter and the shebang line has the python version (3.7.4). I work with Windows 10.
I tried this code in the batch file as well,
"C:\Program Files\Anaconda3\python.exe" "C:\Users\Sathya\Desktop\myscript.py"
pause
All this did was open and close the command prompt in the blink of an eye. I tried to run the bat file a few more times, but got the same result. But, this code worked on a different file (also with tkinter GUI) myscript_test.py and this one gave the GUI window correctly. I don't have a clue as to why it worked on the second one and not the first one.
Code in question:
from tkinter import *
root = Tk()
test_label = Label(root, text = "Hello")
test_label.pack()
root.mainloop()
I can only run this python code from the IDLE, if I run it any other way the window flashes on the screen and closes immediately. I've tried:
-adding an "input" line to keep it from closing
-running from the windows powershell
-compiling the code into an EXE with pyinstaller
and now I can't find any other suggestions. I tried making a simple 1-line program that just asks for input, and that works normally and stays open fine. The tkinter program works fine in IDLE but not any other circumstance.
What's happening here?
EDIT: If I run the program from the command line instead of windows 10 powershell, I get the following output:
Traceback (most recent call last):
File "C:\Users\Cam\Desktop\CSCI Notes\Programs\test.py", line 1, in
<module>
import tkinter
ImportError: No module named tkinter
However, the tkinter file is in the python library on my computer, and importing tkinter in python shell or IDLE works fine.
When I run python on command prompt, it shows python 2.7, so I changed tkinter to Tkinter and the program worked by importing the .py file
Using cmd:
Pyinstaller works(through canopy cmd prompt):
the first line is what I put into my IDLE's cmd prompt
Which populates the folder when executed:
And inside dist, I run test.exe which shows the window
NOTE: my system command prompt is using python 2.7 while the canopy(python environment) command prompt uses 3.5 with pyinstaller
Okay, I think I've solved this one! I read that windows can sometimes try to open .py files with python 2.7 instead, even if they're written in 3.6. I uninstalled python 2.7 from my computer, and now the file runs normally.
So for anybody having this problem, try to make sure your computer is opening python 3, not python 2.
I have also had this problem and as far as I can see no one has a proper solution.
The best I have found is putting -i before your filename. For example:
python -i yourfile.py
this will start the IDLE in the command line
This could also happen if you are using a modulo that isn't installed in your computer even if you have installed it using your IDLE. Try installing it with the command "pip install modulo" else try installing it manually.
I am using python 3.4.2 IDLE on windows.
When I open the IDLE shell and then open .py file, then it works,
but when I try to open the .py file by double cliking, it just doesn't open, or proceed anything. Looks like as if nothing has happened.
I would like to open .py file and then just press F5 to see what is going on rather than individually open all the file (I am still beginner to python, I know I can use pycharm, but at this point, just that would be good enough)
When you double click on a .py file, it will run it using the Python Interpreter. You can right-click on the file instead and choose to open it in IDLE.
I am a complete noob when it comes to programming. I just downloaded python and I will be using Notepad ++. I have saved a file to my desktop and the file name is test and changed the extension from .txt to .py
So when I go to Notepad ++ and create a program and save it, I go to the cmd prompt making sure I am in my desktop directory and type the following
python test.py
and it tells me that python is not recognized. Any help to fix this problem would be greatly appreciated.
First thing python is indent oriented programing language and it comes with its default editor called IDLE. So if you use notepad++ instead of IDLE it might gives you a syntax error. Second thing for executing python file from command prompt,you need to setup environment variable.Please see below link for setting up environment variable.
https://docs.python.org/2/using/windows.html
You can directly run your program in IDLE without using command prompt. So i would suggest you to use pythons built in editor (IDLE)
I have written a program in python that is another front end for un-tarring files on OSX. I want to be able to double click on the file and open with the program that I wrote. How do I make the program automatically know that it is being opened with that file, and to work on that file?
You will need to make your make your script executable with the command chmod a+x <your script.py>
You will also need to tell your OS that the python interpreter is needed to execute this file. In Linux the line #!/usr/bin/env python at the top of the file is what does this. I assume its the same in OSX.
Then right click and select "Open with" and then "Other...". And select your script.
The Python script will need to be configured correctly to run when the OS calls it in this way.
Another way to do it(Caveat: I haven't personally tried this) is to use a shell script as advised in this anwer on the superuser forums.
Python scripts on OS X are not by default recognized as launchable applications by the Finder. What you need to do is use a tool that will package your Python script as a Mac OS X application. Then, you can right-click or ctrl-click a file of your chosen file type and locate that application through the Open With... dialog.
One option I'm aware of for doing this is part of the MacPython package, called py2app:
http://wiki.python.org/moin/MacPython/py2app
So, your steps would be:
1) Use py2app to package your Python script as a launchable application
2) Associate the application with the file type you'd like it to open using by right-clicking or ctrl-clicking one of that file type and choosing "Open With..." in the OS X Finder.
import sys
print("Program.py executing...")
for x in sys.argv:
print(x)
Produces the output
Program.py executing...
Program.py
something.tar
I'm windows, running this command:
python Program.py something.tar
I don't think anything changes from that command to OSX.
And as said by benjamin, the OS is responsible for determining what program is used for the file. I don't know what the command line from your os will be, but you can find out with the above program.