module 'subprocess' has no attribute 'run' when launching as a script

I would like to create a subprocess in python 3.5, and i have this problem :

AttributeError: module 'subprocess' has no attribute 'run'

I searched for the same issue, but in others answers, the problem comes from python 2.7, or version less than 3.5. Here, i am sure to be in python 3.5.2

Here is the command i use to launch script :

(isadora) rdharreville@rdharreville-VirtualBox:~/projects/isadora$ python webbackend/flask-app/subprocess.py 

and the code :

import subprocess
subprocess.run(["ls", "-l"])

The real think that shuffle me is that when i run it in the same virtual environnement, but in the python shell, it works :

(isadora) rdharreville@rdharreville-VirtualBox:~/projects/isadora$ python
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.run(["ls", "-l"])
total 44
-rw-rw-r-- 1 rdharreville rdharreville 17696 juin   6 17:28 bn.log
-rwxrwxr-x 1 rdharreville rdharreville  1114 mai   31 09:11 docker-compose.yml
drwxrwxr-x 4 rdharreville rdharreville  4096 mai   23 17:00 isadora
-rw-rw-r-- 1 rdharreville rdharreville     0 juin   6 17:44 logerrors.txt
drwxrwxr-x 6 rdharreville rdharreville  4096 mai   25 10:49 notebooks
drwxrwxr-x 6 rdharreville rdharreville  4096 juin   6 16:21 resources
drwxrwxr-x 3 rdharreville rdharreville  4096 juin   6 16:21 webbackend
drwxrwxr-x 8 rdharreville rdharreville  4096 juin   5 17:18 webfrontend
CompletedProcess(args=['ls', '-l'], returncode=0)
>>> 

Last thing i did to check if i was in the same python environnement, is to print(sys.version) as the begining of the script, and here is the result :

(isadora) rdharreville@rdharreville-VirtualBox:~/projects/isadora$ python webbackend/flask-app/subprocess.py 
3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609]
Traceback (most recent call last):
  File "webbackend/flask-app/subprocess.py", line 1, in <module>
    import subprocess
  File "/home/rdharreville/projects/isadora/webbackend/flask-app/subprocess.py", line 24, in <module>
    subprocess.run(["ls", "-l"])
AttributeError: module 'subprocess' has no attribute 'run'

So here is the question :

Do you know why this works in one case (python shell), and doesn't works when launching as a script ?

PS : I also tried with metods 'Popen' and 'call' of subprocess module, same problem...

Upvotes: 3

Views: 8592

Answers (1)

nandal
nandal

Reputation: 2634

Change the name of your script subprocess.py to something different.

Because when you are running it as script, there becomes the module with the name of your script 'subprocess'.And python doesn't find run() method inside it.

And when you run the code in python shell, there is not custom 'subprocess' module resulted from your script.

Upvotes: 14

Related Questions