How to Give Python Permission to Write Files - python

I'm learning Python and don't quite have the vocabulary to describe this. However, I can't seem to save files created in Python to my Window10 computer. I discovered this while seeking a help to try to get a file to save in Pandas. I then discovered the same problem when creating a db using SQLITE3 the script seemed to have fun but no database files appeared.
Does anyone know how to fix this? FYI I've got a dual boot Ubuntu machine, I can save files via Python in Ubuntu but really need it to work on my windows machine too.
I am running python via Jupyter Notebook.

I had to make a couple changes to the code snipped that you linked in order to get this to work.
A difference between windows and linux is the file path deliminator is a forward slash:
df.to_csv("tests/ysi_test_files/filehere.csv", index = False)
If you want a hard absolute path to a file, then do something like:
df.to_csv('C://Folder//myfilename.csv', index=False)
Again, if you copy the folder path from a windows folder you will get the backslashes instead of forward slashes. You will need to change those in your code to save the file:
C:\Users\myuser\Desktop\python\
to
C:/Users/myuser/Desktop/python/

If you are running the python script from command prompt then right click on it and run as administrator should solve the issue.

Related

VS Code: "NO FOLDER OPENED"

This may seem trivial, and I admit that it is more a question relating to ease of access in VS Code.
My VS Code window shows "NO FOLDER OPENED". How do I fix this?
Up until the other day when I set VS Code as my default app for .py files (this is the problem), I could see any and all folders, sub-folders, and .py/.ipynb files on my Explorer tab. I am both new to Python and VS Code. I have tried these things to fix my problem:
Attempted to reset to all of microsoft's default app associations--the aforementioned files still open with VS Code.
Attempted to change each file's "Open with:" path (by accessing its properties)--it gives no other option, since PowerShell and CMD are not "apps".
Found this that got close but did not target my specific problem.
Found another article which refers to Windows 7--I didn't read it after seeing this.
I have done various other things, but I think you get the point; I'm at a loss!
This problem seems annoyingly easy to fix. I would rather see if there is some type of setting, in VS Code or Windows, that I am overlooking before searching for a complex work-around.
I'm also new to Stack overflow. I hope this is a "valid" question.
It's important to keep things organized even when you are new to coding. In VScode it is pretty simple to do that. I suggest you to make a folder for your projects and also for other purposes like practicing on examples.
Lets say you created a folder on desktop named Python, and you keep all your .py files in there, then you don't have to open each of the python files by double clicking it, it's a wrong practice. Instead, open VScode application from the start menu or desktop, then go to file > open folder > Python(the python folder you can create on your desktop). This way it opens a workspace(Folder) where you can work with different Python files or any other files.
With Ctrl key pressed, press O and then K. Then, select the folder where your python file is located.

Why can't I run a .py file that only has a print function?

Alright just for context I'm an extreme newbie to python (around 15 minutes of experience).
My question is most likely a dumb one, but I'm gonna ask it anyways as I can't find anything about it elsewhere; Why can't I run the (very advanced) code below as a .py file? Using version 3.6 if it matters.
print("hello")
Thanks
Opening the python file will not cause it to execute.
Depending on your operating system (Windows, Mac, Linux) try opening a
command window and navigating to the file location of you python file.
Once you are in the directory (folder) of the python file that contains
the print statement run the following:
python myfile.py
Does that work?
Download and install
https://www.jetbrains.com/pycharm/download/#section=mac[Pycharm][1]
It is an IDE (Integrated Developer Environment). Pycharm will have what you need to write, save and execute python code all in the same application. There are others of course but Pycharm is as good as any to start with.
Good luck!

Create a desktop icon for a Tkinter script

I have written a python script with a Tkinter GUI. I would like to create a desktop icon that will execute this script so that the end-user (not myself) will be able to double-click the icon and have the GUI load, rather than 'run' the script from the terminal or python shell and then have to F5 from there.
Is there a way to do this? I have googled many arrangements of my question but most answers seem to be normal python scripts, not ones which are Tkinter based.
I am using a Raspberry Pi with Wheezy and Python 2.7
Thanks in advance.
I create executables(icons that I click to start the programs I write) using 'py2exe'. I use windows 7 and I am not sure if it would work for you with Raspberry Pi, but a google search may clear that up for you. I will provide the link below. Basically you end up with a folder with the executable(icon) and also some files, without which the executable won't work. It's unfortunate that you get this extra 'baggage', but it's still handy and the best solution I have come across. I don't think there is a much better way, but I am not 100% on that. Interestingly, I found that you could delete most of these baggage files and the executable would still work, it was trial and error, but it didn't take long. If I want to send the folder to someone, I zip it first.
py2exe is here
If you need a 64 bit binary you can get it here, along with, actually, pretty much every other version. get py2exe here also
Besides creating executable file other option is create simple .bat file:
Open notepad
Enter "C:\ProgramData\Anaconda3\python.exe" "C:\Users\Your ID\script.py"
First part is path to python.exe, second to your python script
save file as .bat file, ex. "open_program.bat"
Now simply double click on saved .bat file icon should open your script.
I like using cx-freeze to convert python scripts to exe.
There is very easy to follow documentation to do this. In short, you create a setup.py script that specifies what libraries and packages you want to include or exclude in your application.
You can then run the build in the console and it will create a build folder, in which will be an Application File. You can create a Desktop short cut using send to and selecting desktop.
Documentation link
You can save the script as a .pyw file so the user can click on the file and the GUi would open

How do I get a python program to run instead of opening in Notepad?

I am having some trouble with opening a .py file. I have a program that calls this .py file (i.e. pathname/example.py file.txt), but instead of running the python program, it opens it in Notepad. How to I get it to run?
The program itself takes in a file, and creates an output that is more readable.
Edit: The operating system is Windows 7. And the file that is calling the python is a .bat file.
Edit 2: It looks like I had to reinstall python for some reason... but it looks like it is finally working. Why reinstalling never comes to mind in the first place... And then I had to change how the file extention was opened. Thanks guys
This happened because most probably you have set notepad as the default program to open a .py file. Go to default programs app in windows. Select choose app by extension. Here search for .py files. Change the option from notepad to python. This should solve your problem.
okay.
1) i tried turning it off and on again.
2) i uninstalled and reinstalled python
still no joy. and then!
in windows explorer there's an open with option that sets the default program that windows is pointed toward if you click on the filename or enter it on the command line. change that from notepad or whatever it is if it's not python. change it to python. then presto. no problem-o.
You need to run it from the command line.
http://docs.python.org/2/faq/windows.html#how-do-i-run-a-python-program-under-windows
Are you trying to run the program like this?
/dirdir/MyPythonScript.py
try the following instead
python /dirdir/MyPythonScript.py

Distributing python application tutorials

Hi as a python newbie I have written a small python application that can convert an excel worksheet into a sqlite database table. Its not a terribly complex application but it does make use of external modules/package like xlrd (http://pypi.python.org/pypi/xlrd) which I had to download and install when writing my app.
Now that is all done I would like to distribute it amongst my friends, all windows users, while they have python on their machines, they may or may not have the xlrd modules.
I would like to package my app, make sure it includes everything that it needs to run, and share the final .zip file with my friends so they can use the application. Is there a good tutorial that covers how to package a python application, with all the necessary external modules/packages, so that another windows user, can easily run my application.
I keep hearing about disutils, can anyone point me to some good tutorials, or any other python packaging tutorials that show how to get everything into a simple easy to distribute file.
Many thanks
Note: I also want to include the sqlite database file with my application, so the end user doesn't have to worry about anything
First download Pyinstaller and save it somewhere. Then, if you're running Python 2.6 or 2.7 go and install pywin32.
Go create a directory for your output file.
Open Command prompt and enter the fallowing:
python path/to/pyinstaller/pyinstall.py file/to/be/converted.py --onefile
If you want to add an icon, add the --icon argument.
If you want it to hide the CMD window add the --windowed argument.
So if you wanted it to use an icon and hide the CMD window it would look like this:
python path/to/pyinstaller/pyinstall.py file/to/be/converted.py --onefile --icon="path/to/icon.ico" --windowed
The --onefile argument makes the saves everything into one file, without it, the output would be would be a lot of files.
The output is saved in the "dist" folder in the directory it was using.
After you convert the python script in to an .exe, put it in a .zip file along with any other files you need (in your case the squite database file) and you can give it to anyone without them needing to have Python at all.
If you want a program to do this all for you, you can download a program called PTEC.

Categories