0

In Ubuntu, I'd like to create a command-line called pycharm_help which will open firefox or another browser with the website https://docs.python.org/2.7/py-modindex.html. I know for doing this, I need to create a script with #!/usr/bin at the beginning. As I'm starting doing programming, I'd like that someone could help me to create this script in python. Could anyone be able to tell me how to do it? And help me create this little program?

Thanks in advance!

  • See: [How to run bash command inside python script?](http://stackoverflow.com/q/26236126/3776858) – Cyrus Jan 08 '17 at 20:24
  • Thanks @Cyrus! I'd like someone to help me to construct this script. –  Jan 08 '17 at 20:32
  • see python module `webbrowser`. It can open web page using `webbrowser.open(url)`. It is standard module so you don't have to install it. – furas Jan 08 '17 at 21:55
  • @furas Are you able to create that script? –  Jan 08 '17 at 23:38
  • first line: `#!/usr/bin/env python` , second line: `import webbrowser` , third line `webbrowser.open("https://docs.python.org/2.7/py-modindex.html")` See doc: [webbrowser](https://docs.python.org/3.5/library/webbrowser.html) – furas Jan 08 '17 at 23:56
  • Ok good, I understand. If you put that as a full answer, I will accept your answer. –  Jan 08 '17 at 23:59

2 Answers2

0

There is standard module webbrowser to open page in default browser

#!/usr/bin/env python

import webbrowser

webbrowser.open("https://docs.python.org/2.7/py-modindex.html")

If you have to open in firefox then maybe you will have to use

#!/usr/bin/env python

import webbrowser

browser = webbrowser.get('firefox')

browser.open("https://docs.python.org/2.7/py-modindex.html")

BTW: Ubuntu will treat script as command-line command only if

  • it has in first line #! with program which it has to use to execute this script
    (so called "shebang" or "hashbang" - # = she/hash, ! = bang)
    ie. #!/usr/bin/env python or #!/usr/bin/python
    (#!/usr/bin/perl, #!/usr/bin/php, etc.)
  • it has "eXecution" privilage:

    chmod +x script.py
    
furas
  • 134,197
  • 12
  • 106
  • 148
0

If you want to use a python script , you can follow the answer of @furas at comments. But you can do it even in pure command line / bash script like this:

#!/bin/bash
xdg-open "https://docs.python.org/2.7/py-modindex.html" &

xdg-open calls the default web browser in your system.

Save the file (i.e charmhelp) under /usr/bin/ directory to be accesible from everywhere, then make it executable using chmod +x /usr/bin/charmhelp and you can run it when you need it as charmhelp

PS: If you save the file in other directory and you want to run it (i.e /name/home) you need to call it either by full path like /name/home/charmhelp or if you are already in name/home you have to run it as ./charmhelp (mind the dot in the beginning).

You could also use links (terminal web browser) directly from terminal like

links -dump "https://docs.python.org/2.7/py-modindex.html" |less

With links the web page will be displayed in terminal.

Much more simpler make an alias:

alias charmhelp='xdg-open https://docs.python.org/2.7/py-modindex.html &'

runit by charmhelp. To make the alias permanent you have to put it in name/home/.bashrc file .

George Vasiliou
  • 6,130
  • 2
  • 20
  • 27
  • Thanks BTW, your answer is very well! I learnt stuff with that answer, but I wanted the answer in Python. –  Jan 09 '17 at 02:26