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!
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
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 .
Related
I have a MacBook air and have tried opening Python in terminal but when I open it, it opens Python interactive mode. Does anyone know how to open Python script mode please.
I’ve tried typing in things such as Python or Python 3 like safari suggests but that didn’t work.
There is no 'script mode'. You can create a Python script using TextEdit or another editor, save it as myfile.py, and then run it with python myfile.py.
for running what you are calling 'script version' of python you should choose a python file to run and make sure is written in the same or in a compatible version to the python you are running it with (python2, python3)
For running an example script:
python main.py
You need to be in the directory containing the file so make sure you are there before running the command. Using python runs the first version of python you installed, so if you want to use an other you should use:
python2 main.py
python3 main.py
etc
Assuming you've stored your script in a file named itworks.py, the simplest thing is to type the command python3 itworks.py in a terminal window after you've moved to the directory containing the script. Alternatively, you can type python3 followed by a space, then locate your python script in the Finder and drag and drop it into the terminal. This will expand to the full path to the file, allowing you to run a script located elsewhere than your current directory. Don't forget to press return...
In older versions of MacOS you could say python, but that uses python 2 which is no longer supported so you should go with python3 for any new development. (With MacOS Ventura, python 2 seems to have been removed.)
If you have multiple versions of python, you can use the command which -a python3 (or python) to see all versions on your PATH, and the order in which they will be found. PATH works on a first-come-first-served basis, but you can override by using the fully qualified path name to an alternative python.
Yet another solution, for when you want a more permanent script you will use many times in the future, is to use a "shebang" line as the first line of your script. For example, I wrote the following tiny demo:
#!/usr/bin/env python3
print('It works!')
The first line says to parse this script with the first python3 interpreter found in your current environment's PATH. You could replace that with an explicit path such as #!/opt/homebrew/bin/python3. Now make the script executable: chmod a+x itworks.py. You can now run the script from the current directory by typing ./itworks.py. (The leading ./ tells your shell you know it's in the current directory, and is intended as protection against trojan horse scripts.) If you want to be able to use the now-executable script from anywhere, add it to a directory on your path such as /usr/local/bin, and you'll be able to run it by just typing itworks.py.
I am using python3.5 to learn how to scrape data from website. And I realize IDLE is slow when the input explodes (like when I using .text to check the contents of the web). So I use Bash to test my scraper.py script.
After I enter python in Bash:
154-76:~ FDSM_lhn$ python3.5
It's hard for me to open a .py file. The only way I know how to do that is:
import scraper.py
which is not convenient because the object I create isn't in that environment. During the test I need to check something in it from time to time.
Can anyone can help me fix this?
If you have a file called scraper.py, you should be able to execute it via python -i scraper.py. This will leave it interactive.
I have a set of python scripts which I run as a daemon services. These all work great, but when all the scripts are running and I use top -u <USER>, I see all my scripts running as python.
I would really like to know which script is running under which process id. So is there any way to execute a python script as a different process name?
I'm stuck here, and I'm not ever sure what terms to Google. :-)
Note: I'm using Ubuntu Linux. Not sure if the OS matters or not.
Try using setproctitle. It should work fine on Linux.
Don't have a linux system here to test this on appropriately, but if the above doesn't work, you should be able to use the same trick they use for things like gzip etc.
The script has to tell what to run it at the top like this:
#!/usr/local/bin/python
Use a softlink like this:
ln -s /usr/local/bin/python ~/bin/myutil
Then just change your script to
#!~/bin/myutil
and it should show up that way instead. You may need to use a hard link instead of a soft link.
Launching a python script using the python script itself (and file associations and/or shell magic) is not very portable, but you can use similar methods on nearly any OS.
The easiest way to get this is using she bang. The first line of your python script should be:
#!/usr/bin/python
or
#!/usr/bin/python3
depending upon whether you use python or python3
and then assign executable permissions to the script as follows:
chmod +x <scriptname>
and then run the script as
./scriptname
this will show up as scriptname in top.
I am trying to make my python script executable without going through the terminal typing like
python test.py
I want to make it able to run when i click on the file.
How i going to do this in my fedora machine.
Add #!/bin/python as the very first line of your file. Or, if you don't know where your python executable is, type which python in a terminal; then copy the result of that and put it after the #!.
Change the permissions of the file so that its executable chmod u+x test.py
i try but it still open back as gedit
Right click on the file in your gnome file browser or desktop.
Select Properties
Go to Open with and choose Python. If you don't see python in the list, add the command. Just type python in the command to be added.
Add #!/usr/bin/env python at the very beginning of file.
Make chmod u+x filename.py
Change your extension from .py to .sh, so your linux distro's UI will recognize it as shell script and try to execute.
It's Nautilus's fault. Open Nautilus (the file manager), go to Menu > Preferences.
Select the "Behaviour" section.
On the field titled "Executable text files", select the option "Execute executable text files when opened".
Add #!/usr/bin/python as the first line of the file and set the permission to executable chmod 755 yourfile.
If you don't have any specific version requirement then using first line as #!/usr/bin/env python will be more efficient and give the execute permission chmod u+x test.py
I know it can be too late, but i did have the same idea. (run python scripts in fedora) and found some trouble. My suggestion to you is to make a launcher with a .sh file, like this:
#!/bin/sh
gnome-terminal -x python yourscript.py
Give the permition to execute with chmod +x file.sh, them click and will run.
[^_~]
I use raspibian os (Linux)
Add #!/usr/bin/python as the first line of the file.py
right click file >> open with >> chose customize >> custom command line >> type python3
execute file with double click is working
I stumbled upon something I just can't figure out. Following situation: I downloaded the python frontend to control dropbox via command line (dropbox.py). I put this file in the folder:
/home/username1/.dropbox-dist/dropbox.py
I made a simple bash script in /usr/bin called "dropbox":
#!/bin/bash
python /home/username1/.dropbox-dist/dropbox.py
Now when i run it following happens:
The whereis for the file:
root#linux_remote /home/username1 # whereis dropbox
dropbox: /usr/bin/dropbox
When i run it:
root#linux_remote /home/username1 # dropbox
zsh: no such file or directory: /home/username2/.dropbox-dist/dropboxd
Yeah. It tells me another username. To be specific: I'm logged in via SSH on this linuxbox. On the remote shell there is byobu running. In byobu runs zsh. Username2 equals the user that I'm currently logged in with on my local linuxbox, with which I connected:
username2#linux_local /home/username2 # ssh username1#linux_remote
Thats how I am connected.
So there must be a variable which was passed to my remote shell from my local shell, and python seems to read it, but I can't figure out which it would be.
Now.. look at that: When I type in the command that I wrote into the bash script:
username2#linux_remote /home/username2 # python /home/username1/.dropbox-dist/dropbox.py
Dropbox command-line interface
So it runs if I do it manually.
Another thing: If I run it with the whole path it works too:
root#linux_remote /home/username1 # /usr/bin/dropbox
Dropbox command-line interface
And it does work if I run it via login-shell, for example using "bash -l" and then trying to run "dropbox".
It doesn't work either if I change the hashbang to "#!/usr/bin/zsh"
Any ideas on this?
whereis doesn't do what you think: it searches a specific set of directories, not $PATH. which searches $PATH so you need to use which to find out which executable will be executed by a given name.
Edit: which as an external program (for shells that do not have a builtin command, such as bash) will not give a right answer for some cases, e.g. shell aliases. The type builtin should be used instead (it also should be available more widely as it's mandated by POSIX, though not necessarily as a builtin).