Save Permissions Python - python-2.7

I am writing a program which tracks pay stub information. It runs fine when running it from source code via the terminal and saves files correctly. In order to distribute it to my client, I compiled the code using PyInstaller to create a 1 file .exe for distribution on Windows 7, and then used Advanced Installer 11.4.1 to create a .msi file for them to install on their platform.
My problem is when running the application after installing the .msi. In the package I distributed a .txt file with the data to load and save to.
When attempting to update the file I distributed the following error occurs:
IOError: [Errno 13] Permission denied: 'testSave.txt'
The code I'm using to try and save the file is:
saving = open(file_name, 'w')
saving.write(data)
Is there a way to tell Python 2.7 to write regardless of privileges, or to make this specific file have basic user privileges when installing?
Thanks.

I modified my program to write to a different location using the system environment and telling Advanced Installer to install the .txt in the Local App Data directory. This allowed me to open the file with 'rw' privileges.
path = os.environ.get('LOCALAPPDATA')
path = path.split('\\')
real_path = ''
print path
for dir in path:
real_path += dir + '/'
print real_path
real_path = os.path.normpath(real_path + 'PayTrakker/testSave.txt')

With Advanced Installer you can set permissions, but you need a licensed edition, i.e. Professional or higher.
You can also build a Professional project in the trial period, to test the permissions support and see if it will work for you.

Related

pycharm + flask surprising working directory

(Modified to answer the questions in the first comment.)
I am using Flask in PyCharm and getting different results than when I run it from the terminal. In particular, the working directory of the Python program is different under Pycharm. So this code works correctly when running flask from the command line and the print os.getcwd() displays '/Users/Wes/Dropbox/Programming/Python/etpruncnt2'.
However if I run the same code in Pycharm the os.getcwd() displays '/Applications/PyCharm.app/Contents/bin' and the file open fails
with IOError: [Errno 2] No such file or directory: 'data/results.json'.
The root of the project in PyCharm is /Users/Wes/Dropbox/Programming/Python/etpruncnt2. I have also added a screenshot of the project structure in the PyCharm GUI.
I suppose I must have the configuration wrong in PyCharm. I have included a screenshot.
What should I do?
#app.route('/running_count')
def running_count():
print os.getcwd()
with open('data/results.json', 'r') as resultFile:
resultData = json.load(resultFile)
return str(resultData)
You Only Have Shown Script Path
How is project structure ? When you are creating new project you are telling pycharm the project root and may you mixed that part :)
I am going to close this. The immediate problem is that the Working Directory in the config was not filled in.
Most default setups for PyCharm do fill that in, but the setup for flask does not.
I have filed a low-urgency bug report with IntelliJ.

Python cannot find file in current directory with PowerShell

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.

Google App Engine Launcher not completing loading

Good day all. First time stack overflow has not previously answered an issue that I have. My problem is exactly like the one posted here, except I am running windows:
Google App Engine Launcher is not running my hello world for Python Mac
The only thing the log gives me is this
2016-08-18 13:39:13 Running command:
"['C:\Users\Kesi\Desktop\Documents\Computer
Science\Udacity\Intro to backend\hello-udacity\main.py',
'C:\Program Files (x86)\Google\google_appengine\dev_appserver.py',
'--skip_sdk_update_check=yes', '--port=8080', '--admin_port=8000',
'C:\Users\Kesi\Desktop\Documents\Computer Science\Udacity\Intro
to backend\hello-udacity']"
Any suggestions are greatly appreciated.
Converting comments to an answer.
The 1st argument in the command list from your message is 'C:\Users\Kesi\Desktop\Documents\Computer Science\Udacity\Intro to backend\hello-udacity\main.py', preceeding the dev_appserver.py suggests a configuration problem.
In the google app engine preferences section there is a place to specify the path to executable python file. That should not be set to your app's python file (as you have it), but to the actual python executable (from your python installation) - which executes dev_appserver.py (the 2nd arg in the command list) which in turn loads the subsequent command list args including one which is the one specifying your app code's location - the last one in your case.
The python executable could be a python.exe or pythonw.exe according to the Executing scripts section of Using Python on Windows doc:
Python scripts (files with the extension .py) will be executed by
python.exe by default. This executable opens a terminal, which stays open even if the program uses a GUI. If you do not want this to
happen, use the extension .pyw which will cause the script to be
executed by pythonw.exe by default (both executables are located
in the top-level of your Python installation directory). This
suppresses the terminal window on startup.

Python Tkinter throwing Tcl error

I am learning basic GUI in Python, and I came across a sample example to read file name from file explorer on Stack Overflow.
from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename)
This particular script is working fine when I am trying to run it in IDLE, but the same is not running if I am trying from command prompt in windows 7.
Python Version: 2.7. Here is the output error which I get.
>>> from Tkinter import Tk
>>> from tkFileDialog import askopenfilename
>>> Tk().withdraw()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\Lib\lib-tk\Tkinter.py", line 1685, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: Can't find a usable init.tcl in the following directories:
C:/Python27/lib/tcl8.5 D:/PyProj/lib/tcl8.5 D:/lib/tcl8.5 D:/PyProj/library D:/library D:/tcl8.5.2/library D:/tcl8.5.2/library
This probably means that Tcl wasn't installed properly
Any pointer to what I am missing here can be of great help.
You just need to copy two folders from tcl folder to the Lib folder
tcl8.5 and tk8.5
In case you are using Virtualenv on Windows I found a solution here: https://github.com/pypa/virtualenv/issues/93
I copied the "tcl" folder from C:\Python27\ over to the root of the new Virtualenv, Tkinter.Tk() shows a new window without throwing an exception.
I am running Python 2.7 on Windows 7.
Hit a similar problem after installing Activestate Python and TCL. I found the following page solved the problem for me: ActiveState Python install problem. The fix was to copy the contents of C:\Python27\tcl into C:\Python27\Lib.
Another potential solution (given by user i-shenl in a different ActiveState thread on the same issue) is to set the environment variable $TCL_LIBRARY to point to the tcl library folder ("C:/Python27/tcl", in the question). If you set this system-wide or account-wide (via System Properties), it will affect other programs that use a TCL Library (if any are installed). If you're using Powershell, you can set this variable in your profile to limit its affects to programs run from the shell.
I hit the same problem on Ubuntu 17.04 with virtualenvwrapper for 64 bit Python 2.7
I add tk and tcl library paths in local postactivate script
Go to your virtualenv: workon your-env-name
Edit local postactiave script with your favourite editor, for ex:
gedit $VIRTUAL_ENV/bin/postactivate
Locate tk and tcl library paths. In postactivate script, export TK_LIBRARY and TCL_LIBRARY with appropriate paths. Add this lines to your script with modified paths:
TK_LIBRARY=/home/kamil/anaconda2/pkgs/tk-8.5
TKPATH=/home/kamil/anaconda2/pkgs/tk-8.5
TCL_LIBRARY=/home/kamil/anaconda2/lib/tcl8.5
export TCL_LIBRARY TK_LIBRARY TKPATH
Restart your virtualenv: deactivate and workon your-env-name again.
If you are hitting this kind of error in a python -m venv NAME kind of virtual environment (and you actually have tcl installed in your system), then you need to export the paths similarly as suggested by Kamil Czerski in a previous post for virtualenv.
To find out what are your TK and TCL paths, run a python script outside of the venv (source):
import tkinter
root = tkinter.Tk()
print(root.tk.exprstring('$tcl_library'))
print(root.tk.exprstring('$tk_library'))
Open your venv configuration file bin/activate and find the place where they export PATH and insert after this (insert correct paths from step 1):
TCL_LIBRARY="/tcl/path/from/step/1"
TK_LIBRARY="/tk/path/from/step/1"
TKPATH="/tk/path/from/step/1"
export TCL_LIBRARY TK_LIBRARY TKPATH
Deactivate (if it was activated) and source again your venv:
deactivate
source bin/activate
The "Tcl missing"-error should be gone.
IDLE is probably setting the path required for TCL. To find out what path is being used by IDLE, compare the output of sys.path from IDLE and without IDLE. Then you can add the location of init.tcl either using an environment variable or programatically. See Xenomorph suggestion.
All you need to do is copy tcl 8.6 and tcl 8.5 from tcl file to Lib file on in python.
Python-tcl-tcl8.5 to Python-Lib
Go to directory in which all of your python dependencies are stored
Example:
Python37
-DLLs
-Doc
-etc
-include
-Lib
-libs
-Scripts
-tcl
-python.exe
Go to tcl folder, copy the tcl8.5 and tk8.5 folder
Paste these folders in the Lib folder
This solution works for Windows 10 users

Create a session secret - Python - Google Drive SDK

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.