I am migrating my code from python2 to python3
and I am facing following issue with python3 -m command.
I have a python script which in some cases may be compiled(.pyc) or non-compiled(.py)
so I am not sure whether it is .py or .pyc
but I wanted to execute it
so in python2 if I had to execute that script I would simply do
/usr/bin/python2 -m <path-to-script-without-extension>
In above case I don't require to specify whether it is .py or .pyc
however in python3 if I do,
/usr/bin/python3 -m <path-to-script-without-extension>
I get
/usr/bin/python3: No module named <path-to-script-without-extension>
another way of executing python script is to do
/usr/bin/python3 <path-to-script>
but in order to get <path-to-script>
I need to know whether the script ends with .py or .pyc
consider following example
root@geek:~# cat /tmp/script.py
#!/usr/bin/python3
print("Python script executed") # This syntax works for python2 as well as python3
root@geek:~# ls -l /tmp/script.py # Making sure the script is executable
-rwxr-xr-x 1 root root 100 Mar 19 05:49 /tmp/script.py
root@geek:~# /usr/bin/python2 -m /tmp/script # The syntax that used to work in python2
Python script executed
root@geek:~# /usr/bin/python3 -m /tmp/script # not working in python3
/usr/bin/python3: No module named /tmp/script
root@geek:~# env PYTHONPATH="/tmp" /usr/bin/python3 -m /tmp/script # Even tried setting PYTHONPATH but didn't work :(
/usr/bin/python3: No module named /tmp/script
root@geek:~# /usr/bin/python3 -c 'import sys; sys.path.append("/tmp");import script;' # Added path to sys.path, and it worked but looks more like a workaround
Python script executed
Is there a way to execute python script whose extension we don't know (it can be .py or .pyc) Thanks in advance