This question already exists:
How to create file association in Windows 10 [duplicate]
Closed 1 year ago.
I have developed a .txt file opener application in Tkinter, python. I created the code to .exe and then I also made a setup.exe with InnoSetup. So now I want that all of the .txt files should be opened with my application.
As far as my coding skill goes on, I think this is not something to handle with Python. I think some PowerShell command or InnoSetup may be able to handle it.
So I have an open function in the application. Something like this:
def open(filepath):
bla bla bla
After some time, I got into my head how it will use my open function situated in my code. Can anyone help me to figure out how I can associate my application with all .txt files?
Please comment if there is a lack of information in this question. I will certainly clarify it.
This needs some workaround with the Windows Registry. This question's answers may be helpful: Associate file extension to python script, so that I can open the file by double click, in windows.
In your case, since it's an EXE, you might need to change the command a bit.
Firstly, run cmd (command prompt) as an administrator.
You can do that by searching for cmd and right clicking it to click the "Run as administrator" option.
The path can be just "C:\WINDOWS\system32>".
I tried changing the "%*", removing it and finally, I got a command which worked!
I tried the following command and it works fine for EXEs:
ftype txtfile=C:\Users\<Your name goes here>\main.exe %1
Or wherever your EXE is located (Copy-paste the path with the filename).
The syntax for EXEs is ftype txtfile=<path>\<filename>.exe %1.
Now, you should have the default app that opens .txt files changed!
The name "txtfile" can be changed using assoc .foo = foofile
You can find more about it in Honest Abe's answer.
You can do the same with the other registered file types also, I guess.
Since you want the setup to be distributed, I think changing something in the python file could help, since it needs the admin powers...
Maybe something like:
import win32com.shell.shell as shell
commands = 'ftype txtfile=C:\Users\<Your name goes here>\main.exe %1'
shell.ShellExecuteEx(lpVerb='runas', lpFile='cmd.exe', lpParameters='/c '+commands)
This question provides more information on how to run command prompt as an administrator: How to run cmd command in Python with admin.
Related
I remember that at some point I used to be able open up the python IDLE from the command line by simply typing "idle" return, which would pop up the python idle editor. I recently got a new machine and installed python, but have to open idle from the windows start menu.
When trying to run "idle" or "idle3" from the command line I get
C:\Users\SSims>idle3
'idle3' is not recognized as an internal or external command,
operable program or batch file.
Can anyone tell me how to get the command line "idle" command set up. Thanks for any help!
There are no step-by-step fixes that I have found online to answer this question, but many that answer specific portions of the question scattered around online. Hopefully, this will help others with the same question in the future, as I know there are many who could benefit from this answer.
If you have selected "Add Python X.x to Path Variables" upon installation then you should be able to run "python" from the command line to start the python editor. Similarly, this will allow you to do the same for IDLE.
First, hit your windows key and type "path" then enter to go to your system environment page in settings.
Next, click "Environment Variables then select "Path" under user variables
There should be a popup that contains a path that looks like this:
C:\Users\SSims\AppData\Local\Programs\Python\Python37-32\
Copy that address then click "new"
Paste that address, then at the end of it type "Lib\idlelib" so that your new variable now looks like:
C:\Users\SSims\AppData\Local\Programs\Python\Python37-32\Lib\idlelib
This should be the path to the folder in which the idle executable file resides.
Note: You can check that path to ensure it is right by pasting it into the windows start menu and it should redirect you to the folder. If it doesn't, you can paste the path to the python folder from earlier that was added automatically upon installation and search around in there until you find the idle executable, then add that folder to the Path variables to get it to work.
You should now be able to run the idle command from command prompt, hope this helps!!
Instead of IDLE use, python -m idlelib command
check the IDLE official document
I have written a python script (lets call it myTest.py) and would like to launch it from the windows command promt.
My python interpreter was added to the PATH environment variable, therefore i can launch it just fine as python myTest.py and it is executed successfully.
I would however like to launch it by simply typing myTest.py in the command promt. But if I do this, the file is opened in PyCharm and is not executed.
I have checked assoc .py which gives me .py=Python.File, as well as ftype Python.File which gives me Python.File="C:\WINDOWS\py.exe" "%L" %*. As far as I understand this, a .py file should be launched with py.exe at the given path.
(If you are as confused as I was by the %L option of ftype, that is not mentioned in its help document: It makes no difference in my case and is equivallent to %0 and %1. Source: https://mail.python.org/pipermail/python-list/2015-November/699263.html)
Can anyone explain why my script is opened in PyCharm and not launched with py.exe? What do I have to do to simply execute it instead?
Edit:
The answer of #Hamed169 solves the problem (thanks!), but doesnt't give any details on why there is a difference between the program that is used to open a file and the one that is returned by ftype.
For everyone that is interested in why there is a difference I have found the following question (has currently no final answer, but quite a bit of information): https://superuser.com/questions/204354/how-do-i-get-ftype-assoc-to-match-windows-explorer
Right Click on a .py file
Click Properties
Click "change" in Opens with:
Select your python interpreter instead PyCharm
Run your .py files and enjoy:)
Thanks a lot Hame
We should change it to the python.exe file path, i.e., (in my case):
C:\Users\NEDA\AppData\Local\Programs\Python\Python38-32
I'm using Blender 2.7 on my mac (OS 10.9.2) and the console won't properly open. If I open blender.app/Contents/MacOS/blender, I get a new terminal window, but it's full of a mix of legible and illegible characters such as "œ˙Ì˛Ä&àÖÄH__PAGEZERO__TEXTÃÃ". No print statements or errors will log there from Blender, either.
Anyone know what's going on?
Thanks!
Edit: I'm also new to terminal and was trying to use "open blender" from the /Contents/MacOS directory :P. If you type "./blender" from the parent directory, it works just fine.
If anyone could shed some light on what's happening or what the difference between typing "./filename" and "open filename" is, that would be awesome.
Blender has various resources it needs to run that are located in the same folder as the binary, it starts with the current working directory to find them when you start blender.
In the terminal you are typing commands, there is a sequence (defined in the PATH variable) to where the command is searched for, prefixing the command with ./ is saying to run the command in the current working directory instead of searching through the PATH list for it.
The command open is meant to open editable files in a suitable editor, it would appear that it gets the idea that it can be handled with the terminal, except the new terminal will start in your home directory leaving blender unable to find it's resources. It's been a few years since I used OSX but it may also be trying to run the blender binary as a shell script. Either way open doesn't handle runnable binaries and isn't designed to.
So the difference is that open blender is like saying that you want to edit the file, but ./blender is actually running an application from the command line.
You may also find it fairly easy to create an applescript that tells the terminal to change the working directory and start blender. This can easily be saved as an application you can start from the finder. Which I think would be (untested) -
tell application "Terminal"
do script "cd /Applications/blender/blender.app/Contents/MacOS && ./blender"
end tell
And if all you want is the python output when you run your scripts you may want to try the script here - it lets you run a script in blender's python console to catch the output.
When you want blender specific help with python scripting ask at blender.stackexchange
Sorry if this answer isn't quite to your question, but having to do with the subject:
listen up all mac users, here is something for you:
with my own "getting annoyed" experience and help from the idea of this guy (sambler) I made a simple app for the purpose of opening blender with terminal.
---Please try it, it is easy to install and super handy---
here is the application, if you want it and here...
..is how to use it:
Navigate yourself to blender.app with finder.
Right click on blender and select "Show Package contents".
Download the app and unzip it.
Drag the app into blender's "Contents" folder.
Drag the app to dock and open for the first time.
(optional) Dance around the room and sing about how fortunate you are to have such an app.
Alternatively here is the applescript source: (currently, with plenty of helpful comments)
set myPath to ((path to current application) as string) --find the path to blenderOpen.app
set myPath to ((characters 1 through ((length of myPath) - 1) of myPath) as string) --rip off the last ":"
set charDelete to (last character of myPath) -- rip off the "blenderOpen.app"
repeat until charDelete = ":" -- rip off the "blenderOpen.app"
set myPath to ((characters 1 through ((length of myPath) - 1) of myPath) as string) -- rip off the "blenderOpen.app"
set charDelete to (last character of myPath) -- rip off the "blenderOpen.app"
end repeat
set myPath to myPath & "MacOS" --find the blender runtime by appending this path
set myPath to quoted form of the POSIX path of myPath -- convert path so terminal understands
(*
why this little if statement down below?
This if statement is here because if a user
opens terminal and runs some command,
then afterwards runs our script,
we want to use a new window so as not
to interfere with the user.
However, if WE open terminal,
than we want to use the window
that terminal just made for us.
*)
if testterminal() then
tell application "Terminal" to do script "cd " & myPath & " && ./blender" -- tell terminal to open new window, and open blender, Voila!!!
else
tell application "Terminal" to tell front window to do script "cd " & myPath & " && ./blender" -- tell terminal to open blender, in the current window, Voila!!!
end if
return myPath
on testterminal()
tell application "System Events" to (name of processes) contains "Terminal"
end testterminal
Is there a configuration file where I can set its default working directory? It currently defaults to my home directory, but I want to set it to another directory when it starts. I know I can do "import os" followed by "os.chdir("")" but that's kind of troublesome. It'd be great if there is a conf file that I can edit and change that setting, but I am unable to find it.
In particular, I've looked into my OS (Ubuntu)'s desktop entry '/usr/share/applications/idle-python3.2.desktop', which doesn't contain a conf file, but points to '/usr/lib/python3.2/idlelib/PyShell.py', which points to config-*.def conf files under the same folder, with 'config-main.def' being the most likely candidate. However I am unable to find where the default path is specified or how it can be changed.
It seems that the path is hard-coded in PyShell.py, though I could be wrong with my limited knowledge on Python. I will keep looking, but would appreciate it if somebody knows the answer on top of his or her head. Thanks in advance.
I actually just discovered the easiest answer, if you use the shortcut link labeled "IDLE (Python GUI)". This is in Windows Vista, so I don't know if it'll work in other OS's.
1) Right-click "Properties".
2) Select "Shortcut" tab.
3) In "Start In", write file path (e.g. "C:\Users...").
This is also my answer here: Default save path for Python IDLE? Let me know if this works!
I've found a solution after looking into PyShell.py:
Create a python file under a preferred directory, in my case '~/.idlerc/init.py', and copy/paste the following lines:
import os
os.chdir('<your preferred directory>')
Pass "-r '~/.idlerc/init.py' " argument to the IDLE startup command, like the following (your exec location and name may vary depending on OS, etc):
/usr/bin/idle-python3.2 -n -r ~/.idlerc/init.py
Just use a shell script such as:
#!/bin/bash
cd /Users/pu/Projects/L-Python
/usr/bin/idle
and run that instead of stock idle. The example is on OS X, adapt to your system.
I'm new to python and learning from 'Dive into Python' by mark Pilgrim (can be found online free)
the answer is in chapter 2.4 - hope he doesn't mind me pasting it here as its also plugging his book and is in the GPL
Before you go any further, I want to briefly mention the library
search path. Python looks in several places when you try to import a
module. Specifically, it looks in all the directories defined in
sys.path. This is just a list, and you can easily view it or modify it
with standard list methods. (You'll learn more about lists later in
this chapter.)
Example 2.4. Import Search Path
import sys
sys.path
sys.path.append('/my/new/path')
It's a good book I am a programmer - usually I find learning from books sends me quickly to sleep - not the case here ....
All I had to do here (Linux Mint 18.2 Xfce) ...
Just add path in line "working directory" = "Arbeitsverzeichnis"
It can change depending on where you installed Python. Open up IDLE, import os, then call os.getcwd() and that should tell you exactly where your IDLE is working on.
One default path is specified in idlelib.IOBinding.IOBinding.dirname or idlelib.IOBinding.IOBinding.filename
Ubuntu
So my idle-python3.desktop
file in /usr/share/applications looks like this:
[Desktop Entry]
Name=IDLE (using Python-3)
Comment=Integrated Development Environment for Python (using Python-3)
Exec=python3 -c "import idlelib.IOBinding, os; idlelib.IOBinding.IOBinding.dirname='/DEFAULT/DIRECTORY';import idlelib.idle"
Icon=/usr/share/pixmaps/python3.xpm
Terminal=false
Type=Application
Categories=Application;Development;
StartupNotify=true
To use it you need to set /DEFAULT/DIRECTORY to your desired directory, copy it with root rights into /usr/share/applications. You can also use it for Python 2 but then you need to replace the 3s with 2s.
ConfigFiles
There are also extensions that can be loaded. These must be modules and you specify them by module name. The config files for IDLE are located in HOME/.idlerc and parsed with a configparser. I did not get further with this.
Here's a way to reset IDLE's default working directory for MacOS if you launch Idle as an application by double-clicking it. You need a different solution if you launch Idle from a command line in Terminal. This solution is a permanent fix. You don't have to rechange the directory everytime you launch IDLE. I wish it were easier.
The idea is to edit a resource file inside of the IDLE package in Applications.
Start by finding the the file. In Finder, go to IDLE in Applications (in the Python folder) as if you wanted to open it. Right click and select "show package contents". Open Contents, then open Resources. In Resources, you'll see a file called idlemain.py. This file executes when you launch idle and sets, among other things, the working directory. We're going to edit that.
But before you can edit it, you need to give yourself permission to write to it. To do that, right click on the idlemain.py and select get info. Scroll to the bottom of the getinfo window and you'll see the Sharing & Permissions section. On the bottom right there's a lock icom. Click the lock and follow the prompts to unlock it. Once it's unlocked, look to the left for the + (under the list of users with permissions). Click it. That will bring up a window with a list of users you can add. Select yourself (probably the name of your computer or your user account) and click Select. You'll see yourself added to the list of names with permissions. Click where is says "Read only" next to your name and change it to "Read & Write". Be careful not to change anything else. When you're done, click the lock again to lock the changes.
Now go back to idlemain.py and open it with any text editor (you could use Idle, TextEdit, or anything. Right under the import statement at the top is the code to change the default working directory. Read the comment if you like, then replace the single line of code under the comment with
os.chdir('path of your desired working directory')
Mine looks like this:
os.chdir('/Users/MyName/Documents/Python')
Save your changes (which should work because you gave yourself permission). Next time you start Idle, you should be in your desired working directory. You can check with the following commands:
import os
os.getcwd()
This ought to be the number one answer. I have been playing around this for an hour or more and nothing worked. Paul explains this perfectly. It's just like the PATH statement in Windows. I successfully imported a module by appending my personal "PythonModules" path/dir on my Mac (starting at "/users/etc") using a simple
import xxxx command in Idle.
I created a script that will tell me what to wear in the morning based on the weather (i.e. rain slicker if it will rain, heavy jacket if it will be cold, etc). I have fairly basic programming experience with python and the script works perfectly, but I want to be able to create a file that I can just double-click from my desktop and the script will automatically run.
My goal is to be able to simply double click [something] in the morning and it will automatically run the script and thus tell me what to wear. How could I go about doing this?
System Specifications:
python
Mac OSX
This worked for me on Snow Leopard:
-Put the python script on the desktop.
-Right click on the script file, and choose "Get info"
-Find "Open With", and choose "Python Launcher" from the dropdown box
Now double-clicking the script file will run the script in a new terminal window.
I'm not sure what versions of OS X come with the Python Launcher application. If you don't have that, you can solve it with a couple extra steps:
-Put the python script anywhere
-Create a shell script on the desktop with one line:
python "/Users/john/scripts/what-to-wear.py"
(Where I've assumed your script is called what-to-wear.py and is in /Users/john/scripts. Be aware that you do need to use an absolute path.)
-Make the shell script executable. In a terminal:
chmod 755 what-to-wear-shell-script
-Double clicking the shell script should run it in a terminal, running your python script.
What you want to do is create an executable file.
I've never used a Mac or Python, but look at this question and the first answer:
How can I create a directly-executable cross-platform GUI app using Python?
Seems http://svn.pythonmac.org/py2app/py2app/trunk/doc/index.html is what you're looking for
Use a batch file to make it automatic
Example :
1. Open Notepad -> type the following.
This one's for Windows..It might give you a hint
:start
C:\Python34\python.exe(your python file location)Your *.py file location.
:end
Save this with a *.bat extension
That's it ..you can configure more on this batch,I guess batch is the automation for day to day script
In Linux/unix based OS , add #!/usr/bin/python3 line on top of your script file with extension .py , if you have python version 3. Or change it to the version installed in the machine
Further , make the file executable by
sudo chmod +x <fileName>
for windows, add windows python path and make the file executable
You want the script to download the weather information online and output the clothes based on your predefined rules?
If this is the case, use urllib to download the page and do some ad hoc parsing over the downloaded html page to get the whether information. And write your logic using nested IF THEN ELSE blocks.