This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Test if executable exists in Python?
Is there a python function to let me detect whether a program is installed in the computer. I have a program that runs an .exe, that part works on windows but to run it in linux you need wine so I need a way for the python function to detect wine.
You can use function os.get_exec_path() to get a list of directories which are set in PATH environment variable. If the executable you are looking for is not present in any of these directories it is correct to assume that the program is not installed.
The code snipped to determine whether there is Wine installed then would look like this:
import os
winePath = None
for directory in os.get_exec_path():
testWinePath = os.path.join(directory, "wine")
if os.path.exists(testWinePath) and os.access(testWinePath, os.R_OK | os.X_OK):
winePath = executablePath
break
If there is Wine installed then the path to its executable file (wine) will be in winePath variable; if not found winePath will be None.
The code is also checking whether the file has correct perrmisions for reading and executing it.
The os.get_exec_path() is available since Python 3.2. In older versions you can use os.environ["PATH"].split(":") instead.
Related
This question already has answers here:
Multiple Python versions on the same machine?
(11 answers)
Closed 3 years ago.
while there are multiple versions of Python installed on a Linux machine, is there a way to mention in the script to be open with a specific version, say, 3.8 even if we issue the #python script.py as opposed to python3.8 script.py ?
I don't want to use Linux alias command. I wanna know if that is possible to be accomplished within the script
Use shebang. #!/usr/bin/python
In the first line of the code with the serial number of Python you want at the end.
You'll need to then run your script like ./my_script.py
You can select the interpreter by adding
#!/usr/bin/env pythonXX
as the first line in your script, provided the version is in the path.
You can also invoke the interpreter directly with the script as the argument.
pythonXX script.py
Depending on your situation
pythonXX -E script.py
might be better. That way the interpreter ignores paths set by environmental variables.
For more details view
https://docs.python.org/3/using/cmdline.html
Every time I'm trying to use Emmet's Expand Abbreviation it gives me errors. First a pop up with Unknown Exception and then another pop up with python script plugin did not accept the script.
I tried reinstalling Notepad++ but that didn't help.
I'm running Windows 8 x64 & Notepad++ 6.6.9. This is the first time I'm using Emmet, after recently used the old Zen Coding. (Which works flawlessly.)
I have python 2.7 installed, if that necessary too.
I had the same problem as you.
This is the solution which worked for me:
emmetio/npp Bug ID #12: python script plugin did not accept the script
Basically, the problem lies in the Python Script plugin which will be automatically installed with Emmet.
Go to this link:
http://sourceforge.net/projects/npppythonscript/files/Python%20Script%200.9.2.0/
and download PythonScript_full_0.9.2.0 package.
Replace Notepad++'s Python package with PythonScript_full_0.9.2.0 and unpack the archive in Notepad++'s MAIN directory. (This is where notepad++.exe is.)
(Do NOT unpack the archive inside the plugins directory. That will not work. The python27.dll file needs to be in the same directory where notepad++.exe is.)
I am using Notepad++ v6.6.9 and it works now.
There is a newer version (PythonScript_1.0.8.0.msi ) available here:
http://sourceforge.net/projects/npppythonscript/files/latest/download?source=files
I had the exact same issue on two different computers, one Windows 7 and one Windows 8, both 64bit. I installed the MSI on the Win8 computer and it fixed the problem.
Check to see if it might be just the shortcut keys that aren't working by going to menuBar>plugins>emmet>expandAbbreviation. If clicking on that works after typing in some emmet syntax, but not when you type the shortcut key, reassign the shortcut keys. Here's what I did:
Look specifically at
menuBar>settings>shortcutMapper
under Main Menu-> "Insert Blank Line Above Current"
and see that ctrl+alt+enter is already assigned there.
Change that to something else (I did ctrl+alt+up), and you'll see that your shortcut keys are working for emmet.
You have 4 objects (2 files and 2 directories) that you have to sort:
Put the python27.dll file into the MAIN directory like so: %ProgramFiles(x86)%\notepad++\python27.dll
Put the others (doc directory, PythonScript directory, PythonScript.dll file) into %ProgramFiles(x86)%\notepad++\plugins\.
This works for Windows 8.
Also change the key shorcut by ctrl + E or some other.
This question already has answers here:
Create a single executable from a Python project [closed]
(3 answers)
Closed 9 years ago.
Is there a way to make python an executable file on a mac, windows, or unix?
Nothing fancy in a GUI, but open a terminal window or a console window and run like somebody executed the application through terminal.
If there's no easy way to do this, can someone direct me to any reading material? Thank you!
You can use PyInstaller (http://www.pyinstaller.org/), there is also py2exe (http://www.py2exe.org/).
My experiences with PyInstaller on Linux show that it tends to place a lot of shared libs which may sometimes clash with your distribution so it's sometimes necessary to trim it down a bit afterwards.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Powershell Python: Change version used
I have both python 2.7 and 3.3 installed on my machine (windows). Say I want to switch back and forth between them; how do I write a script that will set change the path variable so I can switch back and forth easily without doing in manually?
import sys
sys.path.remove('<path to 2.7>')
sys.path.append('<path to 3.3>')
and vice-versa
Rather than change the path variable, it would probably be better to remove them both from your path and symlink the one you want to use in some common folder you have in your path (like /usr/local/bin). It would then be very simple to write a script to switch back and forth between them. Just remove the symlink and do ln -s again.
Edit: Disregard; see linked duplicate answer for a better solution.
This question already has answers here:
Passing options to Python executable in non-interactive mode
(4 answers)
Closed 7 years ago.
I would like to execute a script work.py in Python, after executing some initialization script init.py.
If I were looking for an interactive session, executing python -i init.py or setting PYTHONSTARTUP=/path/to/init.py would do the trick, but I am looking to execute another script.
Since this is a generic case which occurs often (init.py sets environment and so is the same all of the time), I would highly prefer not referencing init.py from work.py. How could this be done? Would anything change if I needed this from a script instead of from the prompt?
Thank you very much.
More generally than in the accepted answer of C0deH4cker, this is discussed in the Python manual in Section 2.2.5 - Cusomization Modules. The basic idea is, to get the location of the special start-up script, one needs to execute the following Python code, e.g. from the interactive session of the interpreter:
>>> import site
>>> site.getusersitepackages()
'/home/user/.local/lib/python3.2/site-packages'
The output should be exactly such a location, in the file sitecustomize.py.
Python has a special script that is run on startup. On my platform it is located at /usr/lib/python2.5/site-packages/sitecustomize.py IIRC. So, you could either put init.py in that directory alongside a sitecustomize.py script that imports it, or just paste the content of init.py in the sitecustomize.py.