Sublime Text plugin can’t find my default browser - python

TL;DR: Sublime Text gets a different response from webbrowser._browsers than my terminal.
This has been driving me nuts. I use a plugin, GitLink which will open a GitHub link from your current file. It relies on Python’s webbrowser tool to open the url. The problem is my default browser is Chrome, but it keeps opening Firefox.
In my terminal, if I launch into python or python3, webbrowser.open_new_tab('https://stackoverflow.com') will correctly launch in Chrome. webbrowser._browsers will correctly list 'chrome' as one of my browsers.
However, in the Sublime Text console, webbrowser._browsers is missing Chrome. It lists all the other browser save for the one I actually want. What gives? How is Sublime Text getting a different list than when I run python in my terminal? How do I get it to match?

There are several things you could try.
1. Set the BROWSER environment variable:
As Keith Hall implied this problem might be solved by setting your BROWSER environment variable. If you don't know how look it up for your version - OSX changed how environment variables get set at some point so there are different ways of doing this for different versions of OSX. However, I am on Linux, and my BROWSER environment variable is not set and ST always opens urls in my default browser, this includes calls to webbrowser.open_new_tab() which (as I assume you saw) is what GitLink uses to open urls, so this may not solve the problem.
2. Modify GitLink (your installed version):
First test if this will work - works fine on Linux.
Copy and paste the following 2 lines into the ST console:
import webbrowser
webbrowser.get("chrome").open_new_tab("http://www.google.com")
If that does not open Chrome with Google.com try:
# google-chrome: Chrome variant.
webbrowser.get("google-chrome").open_new_tab("http://www.google.com")
# macosx: uses the OSX default browser.
webbrowser.get("macosx").open_new_tab("http://www.google.com")
# links: generic; doubtful but worth trying at this stage.
webbrowser.get("links").open_new_tab("http://www.google.com")
The full list of possible values may be helpful.
Another possibility is to use the full path instead, see this StackOverflow answer or try:
# Replace path with your path to Chrome if necessary.
webbrowser.register('chrome_path', None, webbrowser.BackgroundBrowser("/Applications/Google Chrome.app"))
webbrowser.get('chrome_path').open_new_tab(url)
Once that is working:
Install the PackageResourceViewer plugin; after it is installed...
Open the Command Palette and select: PackageResourceViewer: Open Resource
In the list of packages select: GitLink
In the list of files select: GitLink.py
The file GitLink.py will open...
If you save this file (nothing will happen at all if you close it without saving) then a copy of GitLink.py will get saved on your system in this location: ST_CONFIG/Packages/GitLink/GitLink.py - this version of the file will override the version of GitLink.py which is stored in the Gitlink.sublime-package file which Package Control would have installed in the Installed Packages folder. Even if the GitLink package gets updated the version in the .sublime-package file will still get overridden. Not a problem, all you need to do to get rid of the changes made is to delete the folder ST_CONFIG/Packages/GitLink/ which contains the GitLink.py file and ST will start using the version from the .sublime-package file again.
The modification is easy:
Scroll down to the bottom of GitLink.py where you will see the lines:
if(args['web']):
webbrowser.open_new_tab(url)
Just change the webbrowser.open_new_tab(url) line to the following (replacing "chrome" if necessary with the value which worked in the console):
if(args['web']):
webbrowser.get("chrome").open_new_tab(url)
Save the file, the plugin should be updated immediately by ST (check the console for the "reloading plugin" message to be sure if you want). The plugin should now open your urls in Chrome.
3. Open an issue on GitLink's GitHub page:
The issue page is here. State your problem and request a setting be added so that users can specify which browser Python's webbrowser module should use.
I suggest you do this anyway and add a link to this StackOverflow page to your issue for reference.

Related

autocomplete-python unable to find python binary

I am working with python and django in Atom, however I get this error when I want to run the server.
I change the path several times but nothing changed.
Did you try searching for the error message?
By default when you install Python on Windows, it is not accessible globally. In order to be able to access it from anywhere, you need to add your python installation folder to your PATH variables
From the desktop, right-click My Computer and click Properties.
In the System Properties window, click on the Advanced tab.
In the Advanced section, click the Environment Variables button.
Highlight the Path variable in the Systems Variable section and click the Edit button.
Add the path of your python executable(c:\Python27). Each different directory is separated with a semicolon. (Note: do not put spaces between elements in the PATH. Your addition to the PATH should read ;c:\Python27 NOT ; C\Python27)
Apply the changes. You might need to restart your system, though simply restarting cmd.exe should be sufficient.
Launch cmd and try again. It should work.
Also since you're using Atom, you might be able to skip this and set the path to the Python executable in Atom's settings.
I have never used Atom, but most IDE's allow you to do so in the settings.

How to stop Jupyter Notebook from turning urls into active links?

I recently updated from iPython Notebook 3.x to Jupyter Notebook 4.x and it seems that they changed how urls are handled in code cells.
Screenshot of how it works now
Previously, any url within quotes would be treated like any other string. It was marked up in red font and could be edited at will (3rd line in the image). This is how I want it to work again.
Unfortunately, now whenever you have a url (1st line) and you put quotes around it (2nd line) it turns it into an active link for the website. This is infuriating because I can no longer allow me to edit the url because it is being treated as an interact-able object. It even prevents me from modifying the rest of the line of code that the url is a part of. I can't even move the cursor into the line. The cursor just vanishes. To workaround this 'feature' I have to cut and paste the line of code into notepad, edit it, then re-paste it back into the notebook.
I use Google Chrome as my web browser for the notebooks, which is one of the supported browsers according to the website. Could it be that there is a setting in Chrome which is messing things up? Or is it that there is a setting in the Jupyter notebook that I can change to revert the behavior? Or am I just going to have to downgrade to iPython 3.x?

Selenium include mozilla instance

I have a server on which I want to build a script to login to page which is using javascript. I want to use python selenium to achieve the same.
We have a shared drive which contains all the installed binaries and the same has to be included. So when running a python program I won't be using my #!/usr/bin/python instead efs/path../python, similarly all the packages are to be included in this ways. sys.path.append("/efs/path.../selenium-egg-info"). This works good, but as selenium would need firefox included, I could see mozilla in the path, but where are it's binary, exactly which folder to include inside mozilla.
You can think Selenium as launching 'firefox' behind the scenes. You won't see it but it's there and then accordingly opening up the webpage and manipulating things.
How do you think it does all that cool stuff without writing explicit url header etc. So for that you need to have a firefox installed with a physical display(monitor) installed.
You can fake a pyhsical terminal it's just input/output but you AFAIK you need to have a firefox installed. Sad news but that's the way it is.
You don't need firefox executable since it comes with the Selenium
Firefox driver is included in the selenium-server-stanalone.jar available in the downloads. The driver comes in the form of an xpi (firefox extension) which is added to the firefox profile when you start a new instance of FirefoxDriver.
See my another answer here

Linking offline documentation to IDLE - Linux

With internet access, < F1 > on IDLE works fine on Linux. With no internet access of course it does not work. I have downloaded the Python doc as html, but can't get IDLE to refer to it directly.
There is a dialog under "IDLE Preferences" called "Additional Help Sources", but this only enables the user to specify single files for single subjects, not the whole directory structure. I can enter a URL, but I am unclear as to what I should enter as the "Menu Item".
I should add that adding the URL to the local directory to my browser enables me to look at the offline doc, it is just the link to IDLE I am missing.
I found reference to a PYTHONDOCS environment variable, but this does not appear to work on Linux, it seems it is specific to Windows.
You almost did all settings you need to refer to local documentation via menu item. Let's look more precisely of what can be done:
Download documentaion for your Python version as zipped html.
Unpack that archive to you desired directory, e.g /home/username/python-docs.
Go to Idle: Options -> Configure Idle... -> General -> Additional help sources -> Add.
Enter label for the "Menu item" field (it will be a label of a new menu item that will appear in IDLE's Help menu), e.g. "local docs".
Press "Browse" button and choose path to "index.html" in unzipped help sources.
Move to Help -> local docs, it will open your local version of Python documentation.
UPDATE:
I've created a ticket on Python issue tracker that requests enhancement of standard IDLE keys configuration so it could be possible to add key bindings for custom added addiotional help sources menu items.
Actually, IDLE does have code to look for an on-disk copy of the html-formatted Python documentation set but the paths are platform-specific and, in the Linux case, are out-of-date for some distributions at least. If you look at the code in Lib/idlelib/EditorWindow.py (I'm looking at the current Python 3 sources), you'll see that for Linux platforms it looks for index.html in either /var/www/html/python or /usr/share/doc/python-docs-x.y/Doc/. On current Debian systems, for example, the Python doc package is installed in /usr/share/doc/pythonx.y-doc/html. If you install the doc packages and then create a link, IDLE should find the docs off-line when you select Python Docs from the Help manual. For example, for Python 3.3 you could do:
sudo aptitude install python3.3-doc
sudo mkdir -p /var/www/html/
sudo ln -s /usr/share/doc/python3.3-doc/html python
That said, the default locations should be updated (I'll add this to the issue that Rostyslav opened).

Default save path for Python IDLE?

Does anyone know where or how to set the default path/directory on saving python scripts prior to running?
On a Mac it wants to save them in the top level ~/Documents directory. I would like to specify a real location. Any ideas?
On OS X, if you launch IDLE.app (by double-clicking or using open(1), for example), the default directory is hardwired to ~/Documents. If you want to change the default permanently, you'll need to edit the file idlemain.py within the IDLE.app application bundle; depending on which Python(s) you have installed, it will likely be in one of:
/Applications/MacPython 2.x/IDLE.app/Contents/Resources
/Applications/MacPython 2.x/IDLE.app/Contents/Resources
/Applications/MacPorts/Python 2.x/IDLE.app/Contents/Resources
/Applications/Python 2.x/IDLE.app/Contents/Resources
/Applications/Python 3.x/IDLE.app/Contents/Resources
Edit the line:
os.chdir(os.path.expanduser('~/Documents'))
On the other hand, if you start IDLE from the command line, for example, with:
$ cd /some/directory
$ /usr/local/bin/idle
IDLE will use that current directory as the default.
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...").
Let me know if this works!
In Windows 10+, click the Windows Start button, then type idle, and then right-click on the IDLE desktop app and open the file location. This should bring you to the Start Menu shortcuts for Python, and you'll find a shortcut to IDLE there. Right-click on the IDLE shortcut and select properties. Set the "Start in" directory to be where you want default save path to be.
It seems like you can get idle into the directory you want if you run any module from that directory.
I had previously tried opening idlemain.py through the path browser. I was able to open and edit the file, but it seemed like I wasn't able to save my modifications.
I'm just glad to hear other people are having this problem. I just thought I was being stupid.
If you open a module, that sets the default working directory.
Start IDLE.
File -> Open to open your file. And set the current working directory.
In my case, the default directory is set to the directory from which I launched IDLE. For instance, if I launched IDLE from a directory called 'tmp' in my home directory, the default save path is set to ~/tmp. So start your IDLE like this:
~/tmp $ idle
[...]
On Windows (Vista at least, which is what I'm looking at here), shortcut icons on the desktop have a "Start in" field where you can set the directory used as the current working directory when the program starts. Changing that works for me. Anything like that on the Mac? (Starting in the desired directory from the command line works, too.)
For OS X:
Open a new finder window,then head over to applications.
Locate your Python application. (For my mac,it's Python 3.5)
Double click on it.
Right click on the IDLE icon,show package contents.
Then go into the contents folder,then resources.
Now,this is the important part:
(Note: You must be the administrator or have the administrator's password for the below to work)
Right click on the idlemain.py,Get Info.
Scroll all the way down. Make sure under the Sharing & Permissions tab,your "name"(Me) is on it with the privilege as Read & Write.
If not click on the lock symbol and unlock it.
Then add/edit yourself to have the Read & Write privilege.
Lastly,as per Ned Deily's instructions,edit the line:
os.chdir(os.path.expanduser('~/Documents'))
with your desired path and then save the changes.
Upon restarting the Python IDLE,you should find that your default Save as path to be the path you've indicated.
I am using windows 7 and by going to Start-> IDLE(Python 3.6 32-bit)
The click on properties and then in the shortcut tab go to
Start in and entering the desired path worked for me kindly note if IDLE is open and running while you do this you'll have to shut it down and restart it for this to work
If you locate the idlelib directory in your Python install, it will have a few files with the .def extension. config-main.def has instructions on where to put the custom config files. However, looking through these I did not find any configurable paths (your install may vary). Looks like you might need to crack open the editor code to alter it.
If you are using linux, you can create simple .sh file as presented below::
#!/bin/sh
cd /fullPath/PythonScripts/
idle
make the file executable by right click-> properties-> permissions-> check the execute as program checkbox-> done
Run the file :)

Categories