How to run python using ./ from Linux terminal? - python

I have a .py python script and when I run it typing ./filename.py I obtain a syntax error. However, when I run it typing python filename.py my program executes correctly.
How to make it run correctly typing ./filename.py ? I think it is related to the $PATH variable but I don't have any further idea.

put either a shebang at the start of the file, or run with python
for example:
#!/usr/bin/env python3
or run:
python3 filename.py

Related

How do I change the alias of python executable in command line?

Everytime I need to run a python file, I need to enter this into the command line.
python3.7 filename.py
Is there a way I can change the name from python3.7 to just py or something shorter?
alias py=python3.7
py filename.py
Add the alias to you bash_aliases to get it in every terminal
If you're using linux, you can shorten it to nothing by adding the line
#!/usr/bin/env python3.7
to the top of your python file. Then chmod 755 <filename.py> and run it like any other executable.

How to execute python3 program in shell script

I have a python program which I need to run at a particular day of a month, so I am using crontab for this task and create a shell script to run this python program.
This is part of my shell script:
#!/bin/bash
filepath='file2018'
cd ${filepath}
python3 file.py
When I run the crontab which executes the shell script, the log file shows the following error:
line 9: python3: command not found
I'm really confused about why this error occurs because I have already install python3 and I can run python3 directly from the command line.
Besides, if I replace python3 with python, the shell script works! My python version is python2, but I have to use python3 for this program, so I have to use python3 instead of python.
My operating system is Linux CentOS.
Hope someone can give me some tips!
You can give the full path to the python3 executable. You can get it using the which python3 command. Try it out.
in file.py add first line like below and add +x permission to file.py file
#!/usr/bin/python3
it will automatically execute, no need to mention python3 in the script
use "which python3" command to know exact path of python3 in your machine

Bash path tab-complete for python -m

When you run a script, you usually do so as follows:
$ python path/to/script.py
However, when running a module, you run it as:
$ python -m path.to.module
It can sometimes be annoying to run a module from the command line because the . separators keep bash from being able to do tab-completion. Is there a custom tab-completion script out there that could handle this situation?

Changing version of python when using nohup

I am trying to run a python program called compare.py with the linux nohup command which keeps the program running until it is done without interruption. my python program has packages which can only run on python 2.7 and when i use nohup command program is run in python 2.6. how do i change the version of python when using nohup?
Example: nohup python compare.py $
I tried doing:
alias python=python2.7
before starting program and version of python isn't switched. how do i switch the version of python to 2.7 when i run nohup?
The easiest way would be to use a shebang line to specify the interpretter. At the start of your Python file, put something like
#!/usr/bin/python2.7
# This should be a path to an interpreter that you know for sure is Python 2.7
Then, use chmod +x file.py to make the Python file itself executable, and omit the python part of your nohup command, eg. nohup ./compare.py.
I had the same issue with anaconda python. While using nohup python, it was using python 2.7 but generic python command in terminal was taking me to 3.6.
nohup ~/anaconda3/bin/python scriptname.py
Providing full path to python after nohup command will solve the issue

Running python3 programs on osx Lion

I can use python 3 in terminal fine, but I don't know how to make it so terminal will run a program that I have written in python 3.
What do i have to do to associate the .py file extension with python3.2.3 for terminal and not python2.7.1
I am using textwrangler as my text editor, but will happily use any editor if it will run, though I don't think this is my problem as idle doesn't work either and it doesn't have line numbers in it either.
Kind regards
Rob
Add a python3 hashbang to the beginning of your scripts:
#!/usr/bin/env python3
# do stuff
Then, you can make your script executable and run it:
chmod +x script.py
./script.py
try python3 yourprogram.py in your terminal.
or by adding this line on the top of our programs, this is the path to your interpreter:
#!/usr/local/bin/python3.2

Categories