python import from subprocess fails - python

I have been working on some makefiles which call some python scripts to perform various tasks. Up to now, I had always been makeing on a machine with Ubuntu 16.04 (64 bits) installed. Today, I have tried building on my old 32 bits machine with Lubuntu 16.04 using the same makefile and it fails when calling the Python script.
At first, I thought that my Python version was not new enough, but I also have Python 3.5 installed and I changed the line to python3 in the makefile and it still fails:
python3 /home/morane/Documents/Programming/ConnectX/cxPythonTools/RunUnitTests.py -t /home/morane/bin/tests/unit/cxUnitTests.out -l /home/morane/bin/tests/unit/log/unitTests.log
Traceback (most recent call last):
File "/home/morane/RunUnitTests.py", line 41, in <module>
from subprocess import CREATE_NEW_CONSOLE
ImportError: cannot import name 'CREATE_NEW_CONSOLE'
Makefile:51: recipe for target 'unittests' failed
make: *** [cxbaseunit] Error
To test, I have tried to repeat the error directly in the Python shell:
Python 3.5.2+ (default, Sep 22 2016, 12:18:14)
[GCC 6.2.0 20160927] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> from subprocess import CREATE_NEW_CONSOLE
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'CREATE_NEW_CONSOLE'
and it still fails. Does anyone know whats going on?
Regards

It appears that you're running in Linux. The CREATE_NEW_CONSOLE constant is only available on Windows.
https://docs.python.org/3/library/subprocess.html#subprocess.CREATE_NEW_CONSOLE
Specifically the header of section 17.5.4:
The STARTUPINFO class and following constants are only available on Windows.

Related

Issues importing pymssql in Python3 while using conda environment

I am having trouble importing a library called pymssql.
I have read previous solutions regarding this issue on stack overflow. The following are the solutions that did not work:
Error importing pymssql
import pymssql Unicode DecodeError in windows 7
I am running a conda virtual environment with Python 3.7.9. The following is the output when I run python in the terminal.
Python 3.7.9 (default, Aug 31 2020, 07:22:35)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
So, this confirmed that I was using the correct Python version. Next, I tried to import a library called pymssql.
This looked like:
>>> import pymssql
This returned an error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/anaconda3/envs/conda_env/lib/python3.7/site-packages/pymssql/__init__.py", line 3, in <module>
from ._pymssql import *
File "src/pymssql/_pymssql.pyx", line 1, in init pymssql._pymssql
ImportError: dlopen(/usr/local/anaconda3/envs/conda_env/lib/python3.7/site-packages/pymssql/_mssql.cpython-37m-darwin.so, 2): Symbol not found: _iconv
Referenced from: /usr/local/anaconda3/envs/conda_env/lib/python3.7/site-packages/pymssql/_mssql.cpython-37m-darwin.so
Expected in: flat namespace
in /usr/local/anaconda3/envs/conda_env/lib/python3.7/site-packages/pymssql/_mssql.cpython-37m-darwin.so
>>>
I have tried conda install pymssql and pip install pymssql. Neither of these solved the issue.

Properly using Anaconda's Python as the PATH's "python"

I've noticed that while I can point my environment variables to Anaconda's Python interpreter, I don't get a fully working Python environment when I execute python in the command prompt. Python will still launch, but many of its packages are "broken."
PROBLEM:
Anaconda's Python interpreter does launch when called from command prompt, but it isn't able to import some of its packages (numpy being one of them used in this example).
EXAMPLE(WINDOWS CMD):
> where python
C:\Anaconda3\python.exe
Great, python calls the right interpreter. Let's go in there and import numpy!
> python
Python 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
File "C:\Anaconda3\lib\site-packages\numpy\core\__init__.py", line 16, in <module>
from . import multiarray
ImportError: DLL load failed: The specified module could not be found.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Anaconda3\lib\site-packages\numpy\__init__.py", line 142, in <module>
from . import add_newdocs
File "C:\Anaconda3\lib\site-packages\numpy\add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "C:\Anaconda3\lib\site-packages\numpy\lib\__init__.py", line 8, in <module>
from .type_check import *
File "C:\Anaconda3\lib\site-packages\numpy\lib\type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "C:\Anaconda3\lib\site-packages\numpy\core\__init__.py", line 26, in <module>
raise ImportError(msg)
ImportError:
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control). Otherwise reinstall numpy.
Original error was: DLL load failed: The specified module could not be found.
This is weird. Let's try launching "Anaconda Prompt", which appears to just be an activated Anaconda environment.
>python
Python 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>>
Everything works fine this way.
SUMMARY:
I'm expecting Anaconda's Python interpreter to launch from the command prompt and behave just the way it would if launched from within the Anaconda Prompt (which appears to just be a command prompt with an activated Anaconda environment).
What can I do to be able to get a fully working Anaconda Python interpreter by just launching python from the Window's command prompt? Maybe what I'm really asking is, what do I need to change in my environment variables to ensure that the Anaconda environment is activated when I call on python from the command prompt?
Activating a conda environment can also change the PATH. I suggest you compare PATH before and after activating the environment.

ciscoconfparse unable to import python3.7 win10

Any new solution for this. I am unable to from ciscoconfparse import CiscoConfParse
I tried several versions of ciscoconfparse in python3 usin gpowershell in windows10.
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from ciscoconfparse import CiscoConfParse
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python3\lib\site-packages\ciscoconfparse\__init__.py", line 1, in <module>
from .ciscoconfparse import *
File "C:\Python3\lib\site-packages\ciscoconfparse\ciscoconfparse.py", line 4018
^
SyntaxError: invalid syntax
I still have not found a version that will let me import the module.
I browsed through several links here..but did not find an answer. The closest issue to mine is:
ciscoconfparse in Python 3.4 module doesn't import correctly
but not answer.
Any new info?
Thanks
I have reproduced the bug where Win10 and Python 3.7.0 cause problems.
FWIW, if you completely uninstall Python 3.7.0 (including manually deleting your Windows installation directory tree), then install Python 3.7.1, you should not have this problem with Windows 10.

No module named 'Twisted'

I just installed Python to start experimenting a bit with Scrapy.
Found a tutorial to first install Miniconda and with this I was able to install Scrapy. Now the problem is that the software I'm trying to run also requires Twisted. So I installed twisted with pip.
Twisted seems to have been installed correctly but I'm still getting an error when running the script that the Twisted module could not be found.
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
================ RESTART: C:\xampp\htdocs\crawler\crawler.py ================
Traceback (most recent call last):
File "C:\xampp\htdocs\crawler\crawler.py", line 3, in <module>
from scrapy.utils.project import get_project_settings
File "C:\xampp\htdocs\crawler\scrapy\__init__.py", line 43, in <module>
from twisted import version as _txv
ModuleNotFoundError: No module named 'twisted'
>>>
================ RESTART: C:\xampp\htdocs\crawler\crawler.py ================
Traceback (most recent call last):
File "C:\xampp\htdocs\crawler\crawler.py", line 3, in <module>
from scrapy.utils.project import get_project_settings
File "C:\xampp\htdocs\crawler\scrapy\__init__.py", line 43, in <module>
from twisted import version as _txv
ModuleNotFoundError: No module named 'twisted'
>>>
Anybody a suggestion?
Thanks in advance,
You installed twisted for python2. When you use the command pip , you are installing a package for python2. You are using python3 (3.6.3). To install twisted for python3, you have to do:
pip3 install twisted
I hope this helps.
To try if twisted correctly installed, start python interpreter and type
import twisted
If you can't import twisted, you should check if it is in your python path.
Python path can be checked in the python interpreter as
import sys
print sys.path
The directory, where your twisted is installed has to be among those folders.

Debian mitmproxy fails to find/load python-2.7 modules installed via pip

I'm running Debian wheezy and have installed mitmproxy from system packages, and pushbullet.py (https://github.com/randomchars/pushbullet.py) using pip, ie.
apt-get install mitmproxy
pip install pushbullet.py
Pushbullet works when I import from python cmdline, like so:
$ python
Python 2.7.3 (default, Mar 13 2014, 11:03:55)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from pushbullet import PushBullet
>>> pb=PushBullet(myapikeyremoved)
>>>
However, when I try to import the pushbullet module from within a mitmproxy script, it is unable to find the module.
$ echo "from pushbullet import PushBullet" > mypb.py
$ mitmproxy -s mypb.py
Script load error: Traceback (most recent call last):
File "/usr/lib/python2.6/dist-packages/libmproxy/script.py", line 48, in load
execfile(path, ns, ns)
File "mypb.py", line 1, in <module>
from pushbullet import PushBullet
ImportError: No module named pushbullet
Now, pip has installed pushbullet.py and its dependencies python-magic, requests, backports.ssl-match-hostname into /usr/local/lib/python2.7/dist-packages/. It seems that cmdline python has found the pip-installed packages, but mitmproxy is not able to for some reason.
So, I've tried to add the path via PYTHONPATH or via system path, but I think I must still be doing something wrong.
$ cat mypb.py
import sys
sys.path.append('/usr/local/lib/python2.7/dist-packages')
from pushbullet import PushBullet
pb=PushBullet(myapikeyremoved)
$ mitmproxy -s mypb.py
Script load error: Traceback (most recent call last):
File "/usr/lib/python2.6/dist-packages/libmproxy/script.py", line 48, in load execfile(path, ns, ns)
File "mypb.py", line 4, in <module>
pb=PushBullet(myapikeyremoved)
File "/usr/local/lib/python2.7/dist-packages/pushbullet/pushbullet.py", line 26, in __init__
self.refresh()
File "/usr/local/lib/python2.7/dist-packages/pushbullet/pushbullet.py", line 223, in refresh
self._load_devices()
File "/usr/local/lib/python2.7/dist-packages/pushbullet/pushbullet.py", line 32, in _load_devices
resp_dict = resp.json()
TypeError: 'dict' object is not callable
Can anyone shed light on why everything works in via cmdline python and fails when loaded as a script from within mitmproxy, and how to fix it? How should modules installed via pip be loaded?
Thanks to Christian Rapp's comments, I looked further into why mitmproxy's libmproxy was being loaded from /usr/lib/python2.6. It turns out that (at least on the debian wheezy package), the mitmproxy shebang explicitly specifies python2.6:
$ head -1 /usr/bin/mitmproxy
#! /usr/bin/python2.6
That explains why mitmproxy fails to find the pushbullet module; it is looking in the 2.6 directories instead.
I guess that the "TypeError: 'dict' object is not callable" error when I explicitly loaded the module is also because pushbullet relied on some feature of python 2.7.

Categories

Resources