I'm trying to set up a new coding environment for myself so that I may learn Python at home on Windows 10. I already installed Python 2.7 and I am able to run it in PowerShell using the python command. However, when I try to get it to run other files (namely HelloWorld.py) it is getting an error
C:\Python27\python.exe: can't open file 'HelloWorld.py': [Errno 2] No such file or directory
I am in the same folder as HelloWorld.py on PowerShell and when I type ls, it appears on the screen. Most of the other questions I've seen on here were resolved by changing the environment variables in System to include python, but I have already done that. I am looking for anything else to try.
I have first tried to create my original game sample using Pygame.
I have traced the instruction written in the Web site below:
https://irwinkwan.com/2013/04/29/python-executables-pyinstaller-and-a-48-hour-game-design-compo/
Specifically,
I created "myFile" class something like following and read every file (.txt, .png, .mp3, etc...) using this class
myFile Class
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
class myFile():
def resource_path(self, relative):
if hasattr(sys, "_MEIPASS"):
return os.path.join(sys._MEIPASS, relative)
return os.path.join(relative)
### code where I read something ###
myfile = myFIle()
filename = myfile.resource_path(os.path.join("some dir", "somefile"
+ "extension")
I typed command below to create .spec file (myRPG.py contains main)
pyinstaller --onefile myRPG.py
I modified .spec file so exe object include Trees (I store data files in separate
directories such as data, image, and so on)
myRPG.spec file
# -*- mode: python -*-
block_cipher = None
a = Analysis(['myRPG.py'],
pathex=['C:\\mygame\\mygame'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
Tree('bgm', prefix='bgm'),
Tree('charachip', prefix='charachip'),
Tree('data', prefix='data'),
Tree('image', prefix='image'),
Tree('mapchip', prefix='mapchip'),
Tree('se', prefix='se'),
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='myRPG',
debug=False,
strip=False,
upx=True,
console=True )
I did rebuild my package using modified .spec file
pyinstaller myRPG.spec
When I execute myRPG.exe file, I got the error below
C:\mygame\mygame\dist>myRPG.exe
[Errno 2] No such file or directory:
'C:\\Users\\bggfr\\AppData\\Local\\Temp\\_MEI64~1\\item.data' IO Errorが発生しました。
Traceback (most recent call last):
File "myRPG.py", line 606, in <module>
File "myRPG.py", line 37, in __init__
File "myItemList.py", line 11, in __init__
File "myItemList.py", line 33, in load
UnboundLocalError: local variable 'fp' referenced before assignment
Failed to execute script myRPG
I believe that I properly specify the directories where data is expanded since I use the function that checks _MEIPASS but it does not work.
I have also tried to use "added_files" instead of Tree did not help for me at all. ”a.datas" may work for small number of files but I do not want to specify all the files I'm going to use, because it will be over hundreds of thousands of files.
This is a very helpful page that I used when converting my pygame program into an executable.
In short:
Here is the place where you can download pyInstaller. Click on the download button under "Installation." Wait for it to download, then run it.
Type this into the popup where it says command: venv -c -i pyi-env-name. The reason for this is stated in the page mentioned at the top.
If you have pyInstaller installed already, type pyinstaller --version to make sure. If you don't, type pip install PyInstaller. If this doesn't work, look at step 7 in the guide up top.
Make sure that you put a copy of your program in the folder that it says on the command line.
If you want a zip with the exe and all necessary files in it type pyinstaller and then the name of your program, complete with the .py extension. However, if you want an exe that doesn't need all of the extra files in the zip that you can just run by itself, type pyinstaller --onefile --windowed and then the name of your program, complete with the extension of .py.
Once you have done this, the exe will appear in a dist folder inside of the one where you placed your program in step 3.
This is how it worked for me, and I apologize if I have misinformed you. Be sure to upvote/mark as answered if this works.
When I run my script from Idle my program runs perfectly, but when I run the .py file and it spawns a shell # C:\Python27\Python.exe my program fails with the following error:-
IOError: [Errno 13] Permission denied: 'my new file.html'
And the bit of code is:-
f = open("my new file.html", "w")
I have searched for this IOError but the things people are saying don't appear to tie in with what I am doing, which is writing out a file?
If it says "permission denied", that's telling you that you don't have permission to create that file. It isn't lying to you. The first rule of debugging is to always assume the error is telling you the literal truth.
Since you didn't supply a folder in the filename, it's trying to create a file in the current directory. You are probably in a protected folder. If you cd to a folder where you have write permissions, the problem will likely go away.
Using: Windows 7, Python 2.7
Code:
Dr Edit - Google's example for creating a new file in Google Drive
Dr Edit Using Python with Google Drive SDK
The README instructions state:
Create a session secret, which should be at least 64 bytes of random
characters, for example with
python -c "import os; print os.urandom(64)" > session.secret
I have no idea how to run this command line. I've tried to run it in the Windows Command Prompt. I've tried to run it in Python Shell. I've tried to run it in the Python Commmand Line. SyntaxError: invalid syntax
Is this README doc correct? Is it outdated? Am I doing something wrong? Is this a line of code that is supposed to be part of a python program? What is the -c for?
I just realized the the python -c is probably just notation to indicate that the user should use the python command line to enter what follows.
So I entered:
import os
print os.urandom(64)
And a string of special characters were printed:
タAËDkæ4ÃZヌTツUルヒL゚é-ツؓルト0ᄚ'Ënᄚô#ߝUíèRKRüÏ*'ᄇᄂヤナ%ÿëᄄÄÓò&
There is probably supposed to be a session.secret object? file? variable? that the string of special characters get written to.
I needed to learn the Python Print function.
You can read and write files in Python without importing a library.
The Open() function in Python will open a file, OR create a new file
There are four modes to open (create) a file
'r' read only
'w' write only
'a' append
'r+' both reading and writing
This will create a new text file named "daSecretCode" if there wasn't one already. NOTE: The file gets created in the directory from where the .py module was run from.
daNewFile = open("daSecretCode.txt", "w")
Write content to file:
daNewFile.write("oerjfwi456745oetr78u9f9oirhiwurhkwejr")
Close the file:
daNewfile.close()
Write a new session secret to a new file:
import os
daSecretCode = repr(os.urandom(64))
daNewfile = open("daSecretCode.txt", "w")
daNewfile.write(daSecretCode)
daNewfile.close()
I'm not sure how I could format the content to look like the original output. The characters get formatted into something else.
I am trying to run my celcius.py file but I don't know what to type in the command line...
When I type Python celcius.py it does not work:
I get the following message:
>>> python celcius.py
File "<stdin>", line 1
python celcius.py
^
SyntaxError: invalid syntax
>>>
When I open it the program with the Python IDLE GUI it runs the program and closes the program before I can see it. I read somewhere that it won't close if you run it from the command line.
You need to type "python celsius.py" in the windows command prompt, rather than the Python command prompt; this only works straight away if the Python directory is in your PATH. It probably isn't, so you need to use this instead: C:\Python27\python.exe celsius.py.
Because celsius for you is in the same folder as python, you can do this as well:
CD C:\Python27
python.exe celsius.py
In general, change the directory (CD) to where your file is stored, then run C:\Python27\python.exe <filename>
Double-clicking is generally easier in Windows, though, assuming it is set up to do that (it normally is unless you change it back)
There are a few ways of doing this.
WAY 1
step one: Open command prompt (go to start and type in "cmd") *NOTE: do NOT open python. if you see ">>>" in your command prompt window you opened python. This is bad.
step two: type in this: python "path\to\file\celcius.py" replacing path\to\file with the path (starting with the name of the folder that you're computer user is named. So for my computer that would be ben. So let's say my celcius.py is located on the desktop I would run python "Desktop\celcius.py" and hit <.enter.>. It looks like you have celcius.py in your python folder. Don't put it there. that's the system files for python itself. move it to your desktop or your documents folder. By the way, if you have truble using this, make sure you're system is set up to execute the python command. Go here for reference.
WAY 2
assuming you have python associated with the .py file in explorer, you can find the file and double-click on it.
WAY 3
open IDLE (you can usually find it in your list of programs in start). Open the python file through IDLE. This will show you the source code for your program. hit <.F5.>. F5 will execute the program from within the the IDLE shell.
You don't need to do this, just open the command prompt and type:
python path/to/your/file.py
Just make sure that python command is set in your environment PATH, check this