0

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

  • 1
    I believe the problem may be that your `sys.path` variable needs to be updated to include the folder path(s) where you expect to find the `.py` file(s). It shouldn't matter if modules are compiled. I believe you can set PYTHONPATH to include these folders, and `sys.path` should have entries for them. Also you can dynamically append folder paths to `sys.path` at runtime. – Todd Mar 19 '20 at 07:15
  • Yes, in a nutshell, Python doesn't see your module in any of the paths it looks for, that's your only problem. – deceze Mar 19 '20 at 10:32
  • `PYTHONPATH=/tmp /usr/bin/python3 -m script` should work – rolf82 Mar 19 '20 at 11:00
  • Thanks @Todd, deceze and rolf82, for your solutions. Now I understood one major difference between python2 and python3 in terms of importing modules. Even after updating PYTHONPATH, I was importing it in wrong way. thanks for correcting me and helping me out. – Rohan Yadav Mar 19 '20 at 11:27
  • in the shell, `export PYTHONPATH=${PYTHONPATH}:/home//` will update the variable. – Todd Mar 19 '20 at 17:12
  • you can also add that as a line to your `.profile` or `.bashrc` file. – Todd Mar 19 '20 at 17:14
  • Thanks @Todd, I tried it out and it worked. Thanks! – Rohan Yadav Mar 20 '20 at 09:20

0 Answers0