No module Named Subprocess - python

I am writing a program in python and it came back with this error. I have tried the solutions from other questions but none of them work for me hence why am asking again. I am using python on the Raspberry Pi running Rasbian.
Python 2.7.3 (default, Mar 18 2014, 05:13:23)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Traceback (most recent call last):
File "/home/pi/Desktop/Docs/rc/serial transmit.py", line 2, in <module>
import Subprocess
ImportError: No module named Subprocess
>>>
Thanks in advance to anyone who can get me subprocess back :)

subprocess is not capitalized. Try
import subprocess

It's important to use proper case for identifiers in python. Subprocess and subprocess are two different modules from python's point of view, and only second one is a part of standard library. Variables are also case sensitive in Python. And since you're using raspberry pi I would add that case sensitivity also applies to Unix filesystems. So, that should work:
import subprocess

Related

The imported modules get unimported after running a python file [duplicate]

This question already has answers here:
How to prevent python IDLE from restarting when running new script
(2 answers)
Closed 3 years ago.
I noticed that all the modules I import are removed (I can't use them anymore without importing them again) after I run a python file in the python IDE.
Here is the view of my IDE:
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import os
>>> os
<module 'os' from 'C:\\Users\\MN\\AppData\\Local\\Programs\\Python\\Python37\\lib\\os.py'>
>>>
RESTART: C:/Users/MN/AppData/Local/Programs/Python/Python37/python file just ran.py
A python file just ran
>>> os
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
os
NameError: name 'os' is not defined
>>>
For deep view, this was in my file:
print("A python file just ran")
Why is this happening and how to make the imported modules stay in there without importing them again?
I'm not sure if you can make them stay, sorry.
Python restarts when you launch a file in IDLE (it even states RESTART).
Edit: Try defining variables, methods or classes - they too will be gone, not just your imports.
Edit two: You can import your file (python will automaticly execute it), that will not restart python so you will keep what you defined/imported.
Maybe just to add why this is the case...
Every Python program must have some module that is in "control" of the program. This is the same as in C where you have a "main".
If you want to use functions from file_just_ran.py, then you should define those as functions and then import them into your session. Then the session is in control.
If you want the scrip to be in control, then you must do these import etc either in the script, or in a file that that script can import.
Otherwise, how does the interpreter know what to do? You're trying to execute commands in a running python program?
If you just want to run scripts and code in an interpreter in parallel, then open two sessions.

Why is my script's directory not in the Python sys.path?

Python 3.6.5
I am aware of this one: Why does my python not add current working directory to the path?
But the problem there is that he's doing something more complicated (referring to a sub-folder but executing from a main folder). The answers there are to either simplify things or to add package definitions.
And the selected answer even says: "It is the script's directory that is added"
However, my problem is really more simple: My script's directory ISN'T added.
Basically, all the tutorials on the internet say: import mymodule
When I do that, I get a name error...
My folder structure:
C:/Projects/interner
interner.py # this is the main program body
aux.py # this is the auxiliary file I would like to import into the above
I've tried both coding 'import aux' inside interner.py, and also using the interactive console:
cd c:/Projects/interner
python
import aux
To no avail (ModuleNotFoundError: No module named 'aux')
My sys.path:
['C:\\Tools\\Python\\python365\\python36.zip', 'C:\\Tools\\Python\\python365']
(both from inside the script and from interactive console)
Could you please tell me why I can't import local scripts? Is it because my sys.path is missing the PWD? If so, why is it missing it?
Edit: Doing this to help investigation:
>>> import os; print(os.listdir("."))
['aux.py', 'hw.py', 'interner.py', 'my_funcs.py']
I believe this is a Python bug, specific to the embeddable (ZIP file without an installer) Windows distribution. I’ve filed https://bugs.python.org/issue34841.
Removing the python37._pth file (presumably python36._pth in your case) from the distribution fixed it for me.
I don't know why but it seems that "" is missing from your sys.path variable, and that prevents from importing modules from current directory all right!
I can somehow reproduce your issue (eatcpu.py is in my current dir):
$ python.exe
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', 'C:\\Windows\\system32\\python27.zip', 'D:\\AppX64\\Python27\\DLLs', ... etc...]
>>> import eatcpu
works. Now in another python session:
$ python.exe
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.remove("")
>>> import eatcpu
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named eatcpu
>>>
So the quickfix for you is to do:
import sys
sys.path.append("")
It looks like you are using the embeddable distribution of CPython rather than one of the regular installers. As described on the documentation page:
The embedded distribution is a ZIP file containing a minimal Python environment. It is intended for acting as part of another application, rather than being directly accessed by end-users.
Since you seem to be directly accessing Python rather than embedding it, you should consider using the regular (or Microsoft Store) installer (also described on the page I linked above).
Try making it explicit:
from . import aux

iPython: DLL load failed: The specified module could not be found; plain Python fine

I keep getting this (well known) error in iPython. Yet, the same import works fine in plain Python. (Python 3.3.5, see details below)
iPython:
Python 3.3.5 (v3.3.5:62cf4e77f785, Mar 9 2014, 10:37:12) [MSC v.1600 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.
IPython 2.0.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import test1
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-7-ddb30f03c287> in <module>()
----> 1 import test1
ImportError: DLL load failed: The specified module could not be found.
Python (not only it loads fine, it also works):
$ python
Python 3.3.5 (v3.3.5:62cf4e77f785, Mar 9 2014, 10:37:12) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import test1
>>>
Now, Dependency Walker on test1.pyd shows this
[ ? ] LIBGCC_S_DW2-1.DLL Error opening file. The system cannot find the file specified (2).
[ ? ] LIBSTDC++-6.DLL Error opening file. The system cannot find the file specified (2).
[ ? ] PYTHON33.DLL Error opening file. The system cannot find the file spec
I even overwrote sys.path in iPython with the one from plain Python. The file test1.pyd is in C:\Test.
['c:\\Test',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\python33.zip',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\DLLs',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\lib',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\lib\\site-packages',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\lib\\site-packages\\FontTools',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\lib\\site-packages\\win32',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\lib\\site-packages\\win32\\lib',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\lib\\site-packages\\Pythonwin']
Why would the import work in plain Python but not in iPython?
I have encountered the same problem. After hours looking and thinking I found out the cause. The difference is environment variables between interpreters (plain python and ipython or pycharm etc.). I think your can use %env in ipython to check the environment variables. In plain python, use (works in python 3.7):
import os
os.environ
Then if there are differences, maybe you should set the right one before your run.
Actually there are multiple ways to set envs. For example
os.environ['key']='value' #Both key and value are strings
or
os.putenv('key', 'value')
Here key is the name of the environment variable, and value is the value you want to set it to.
Hope this helps you.~~~///(^v^)\\~~~

Writing Python script in PowerShell: how to use argv?

I'm a beginner to Python. I've already tried to search for the answer to this question for a long time but had no luck with that. Anyhow here's the code that I want to run (I saved it as test.py):
from sys import argv
script, firstArgument = argv
print "Hello %s!" %firstArgument
It works perfectly fine if I type this into PowerShell:
>> cd ~
>> Python test.py "StackOverflow"
However if I try to type the code into PowerShell like this:
>> cd ~
>> Python
>> from sys import argv
>> script, firstArgument = argv
I get a huge error at that point... I'm just wondering, is there a way to declare argv directly into the PowerShell or is it just not possible?
-Thanks
When you invoke python on its own to get it into interactive mode, there are no arguments passed in. Therefore, argv is empty.
I'm running on Ubuntu, but it's the same general concept in Windows:
:~ $ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from sys import argv
>>> print argv
>>> ['']
Powershell and Python are two separate things, each with their own concepts of the arguments passed to them. Just as a bash script has the concept of passed-in arguments, so too does Powershell, though they are handled differently. However, neither is directly connected to Python's argv.
In general, one can use environment variables to pass things back and forth between Python and the shell (i.e. you could set environment variables inside of Python that could be read in the shell or vice versa, but in either case, these would be different from argv, which is specifically the arguments passed in to the python executable). Or one could use environment variables from the shell to determine what arguments should be passed to python, thereby determining what argv will be.
See this link for more detail: https://docs.python.org/2/library/sys.html#sys.argv
Edit in response to comment from OP:
You can't declare argv, per se. It's representative of how the interpreter was invoked, and in interactive mode, by definition, it has no arguments.
For your specific example, it has to do with the way tuples are unpacked in Python and the current state of argv, which is empty string.
:~ $ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from sys import argv
>>> firstArgument=argv
>>> print firstArgument
['']
>>> firstArgument, secondArgument=argv
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: need more than 1 value to unpack
Since argv only has one element in it, you can't assign it to two variables.
You could, for example, do this:
>>> firstArgument, secondArgument=argv,1
>>> print secondArgument
1
Now there actually are two values -- the tuple ('', 1), and so it can be unpacked into the two different variables.

where is the 'itertools' file

import itertools
print itertools#ok
the code is ok
but i can't find the itertools file.
who can tell me where is the 'itertools file'
my code is run python2.5
import itertools
print itertools.__file__
Traceback (most recent call last):
File "D:\zjm_code\mysite\zjmbooks\a.py", line 5, in <module>
print itertools.__file__
AttributeError: 'module' object has no attribute '__file__'
>>> import itertools
>>> itertools.__file__
'/usr/lib64/python2.6/lib-dynload/itertools.so'
The fact that the filename ends with .so means it's an extension module written in C (rather than a normal Python module). If you want to take a look at the source code, download the Python source and look into Modules/itertoolsmodule.c (you can also view it in your browser at http://svn.python.org/view/python/trunk/Modules/itertoolsmodule.c?view=log).
Edit (as an answer to the comment below): it also works with Python 2.5:
Python 2.5.2 (r252:60911, Oct 5 2008, 19:29:17)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import itertools
>>> itertools.__file__
'/usr/lib/python2.5/lib-dynload/itertools.so'
If you're looking for the source file (in C, of course), it's for example online here.
For the windows users (I'm running Python 2.7.2, Win7x64, default installer package) the call to __file__ will flame out as #zjm1126 has noted, I suspect the problem being that itertools is a builtin on the windows package. If you'd picked say, exceptions? You'd get the same behaviour on another platform (e.g. Python 2.6.1 on my macbook) - Windows just happens to have a few more builtins like itertools.
It's not strictly an answer as such, but you could parse sys.modules which would give you a hint as to where it's coming from:
>>> import sys
>>> sys.modules['itertools']
<module 'itertools' (built-in)>
which points at itertools being built-in to your python executable.
Also, the imp.find_module response is providing the same information: the weird return tuple is by spec (see: http://docs.python.org/2/library/imp.html#imp.find_module) and telling you that the module is of type 6, which is the enumeration for imp.C_BUILTIN
try this
>>> import imp
>>> imp.find_module("itertools")
update:
since yours is None, another go through a manual way. Do a sys.path
>>> import sys
>>> sys.path
['', '/usr/lib/python2.6/lib-dynload' ]
then depending on your system, use your system's search facility to find it. on my linux system
$ find /usr/lib/python2.6/lib-dynload -type f -iname "*itertools*"
/usr/lib/python2.6/lib-dynload/itertoolsmodule.so
OR, just search the entire system for the file with name "itertools".

Categories