Running docker from python's venv - python

I have created a python module. Works on ubuntu/xenial with python3.5. I built it and uploaded to python repository. Then I created virtual environment, installed this module and tried to run. As a result I got:
(hdfnc_venv) svc#PL1LXD-529389:~/$ python3 -m my_package.my_module -h
Traceback (most recent call last):
File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/svc/hdfnc_venv/lib/python3.5/site-packages/my_package/my_module.py", line 4, in <module>
import docker
File "/home/svc/hdfnc_venv/lib/python3.5/site-packages/docker/__init__.py", line 2, in <module>
from .api import APIClient
File "/home/svc/hdfnc_venv/lib/python3.5/site-packages/docker/api/__init__.py", line 2, in <module>
from .client import APIClient
File "/home/svc/hdfnc_venv/lib/python3.5/site-packages/docker/api/client.py", line 222
f'Error while fetching server API version: {e}'
^
SyntaxError: invalid syntax
So apparently there are multiple issues here. One is this invalid syntax which I suppose is because python3.5 does not recognize f-string syntax introduced in python3.6. Wasn't there an f-string, an exception would be raised anyway.
The bigger problem is why it there is error with just importing docker. Docker is properly installed on this machine, so is the module docker (5.0.3) in my venv. The module works when I run it directly in the system from the source code, not from my venv installation. Also when I run it this way, python3.5 works just fine, so invalid_syntax is not a problem anymore - most likely python didn't reach this part of the code, which means it managed to import docker module (also 5.0.3) without any problem.

Related

Install Ansible Windows Machine

I have tried installing ansible through pip install ansible but whenever i get the following error trying to use it:
Traceback (most recent call last):
File "<frozen runpy>", line 198, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File "C:\Users\ruan.greyling\AppData\Local\Programs\Python\Python311\Scripts\ansible.exe\__main__.py", line 4, in <module>
File "C:\Users\ruan.greyling\AppData\Local\Programs\Python\Python311\Lib\site-packages\ansible\cli\__init__.py", line 42, in <module>
check_blocking_io()
File "C:\Users\ruan.greyling\AppData\Local\Programs\Python\Python311\Lib\site-packages\ansible\cli\__init__.py", line 34, in check_blocking_io
if not os.get_blocking(fd):
^^^^^^^^^^^^^^^
AttributeError: module 'os' has no attribute 'get_blocking'
I have python and pip installed on the machine.
It looks like this may be a version compatibility issue with Python 3.11. I recommend upgrading to the latest version of Python and then running the command again.
This is why you are getting this error.
Windows without WSL is not natively supported as a control node (more in the documentation)
If you want to run the Control node on Windows use WSL
Good practice code repository and sync to your Linux Control node from Windows machine.

Trouble downloading Python modules

I was making a Tetris game and at the top of the Python code, I put in import pygame, since that was the thing I was thinking of doing and the thing I thought I could only do. My game was complete, the terminal was open, and I ran the code. The terminal gives an error:
Traceback (most recent call last):
File "[removed for safety]", line 1, in <module>
import pygame
ModuleNotFoundError: No module named 'pygame'
So I tried pip installing it with the command pip install pygame, but it just returns a similar error:
Traceback (most recent call last):
File "C:\Python34\lib\runpy.py", line 170, in _run_module_as_main
"__main__", mod_spec)
File "C:\Python34\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Python34\Scripts\pip.exe\__main__.py", line 5, in <module>
File "C:\Python34\lib\site-packages\pip\__init__.py", line 1, in <module>
from typing import List, Optional
ImportError: No module named 'typing'
I was using
VSCode, with Python version 3.10.5, 64-bit.
First of all you can uninstall python 3.4 from your system, with this link then you can reinstall python from from the python.org website. Then hopefully pip install pygame would work. Or, if pygame is installed run pip uninstall pygame then pip install pygame
File "C:\Python34\lib\runpy.py", line 170, in _run_module_as_main
Python34 is mentioned in the error code, which indicates that the terminal runtime retrieves python3.4 instead of 3.10 you mentioned.
This indicates that there are multiple versions of Python in your environment.
You can manually select the Python interpreter by using shortcut "ctrl+shift+P" and typing "python: select interpreter" or click this icon to change the interpreter:
After choosing the correct interpreter, you can use command pip install pygame to install python package at the correct python-lib.

grpc import bug on arm linux system - ImportError: cannot import name 'cygrpc' from 'grpc._cython'

I'm trying to get a grpc (grpcio) service to run on an armv7 system running linux (openembedded poky).
As our it-dep. doesn't allow us to connect our dev boards to our network/internet I'm ushin wheels to install grpcio (+six as it depends on it). The installation works fine, but whenever I try to import the lib in python it fails:
Traceback (most recent call last):
File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/home/root/rp_py_sw/sc_rp_sw.py", line 6, in <module>
import grpc
File "/usr/lib/python3.8/site-packages/grpc/__init__.py", line 23, in <module>
from grpc._cython import cygrpc as _cygrpc
ImportError: cannot import name 'cygrpc' from 'grpc._cython' (/usr/lib/python3.8/site-packages/grpc/_cython/__init__.py)
This is with py3.8.5
Using py 3.6 it worked fine.
Before I used py36 as there where no arm builds for >3.6 for some time. Now that there are some newer builds I thought about switching to py3.8 or 3.9 but for some reason it fails with the error above.
I should mention that I generate the os using yocto, which also builds and includes py3.8.5.
py3.6 (the working one) was build by myself. Has anyone an idea why it won't work after the update, is there something wrong/missing in yoctos py3.8.5?
This two commands would help:
(1)pip install grpcio
(2)pip install cygrpc

Python error - cannot import my requirements

I'm trying to use several imports inside of a Python file. However, none of them seem to be imported correctly. I'm using a miniconda environment, and I've tried running Python versions 2.7 and 3.8 .
The following lines cause failures as soon as I run my app. If I comment-out the first couple of lines, I just get the same error about openpyxl instead of dotenv.
from dotenv import load_dotenv
load_dotenv()
from openpyxl import Workbook
I have both of them listed in a requirements.txt file, and I'm able to run "pip install -r requirements.txt" without issues, but I still get errors when I try to run "python -m src.main".
The errors just look like this (below). Any ideas on what else I can try?
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/Users/path/a/b/src/main.py", line 2, in <module>
from dotenv import load_dotenv
ImportError: No module named dotenv
... or this:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/Users/path/a/b/src/main.py", line 5, in <module>
from openpyxl import Workbook
ImportError: No module named openpyxl
EDIT:
Here's some additional information. I'm thinking these paths got corrupted along the way. Can someone tell me the expected output of these commands, inside of a miniconda environment?
(MyEnv2) MyMacbook:my-rep my-user$ which python
/usr/bin/python
(MyEnv2) MyMacbook:my-rep my-user$ which pip
/Users/my-user/opt/miniconda3/envs/MyEnv2/bin/pip
(MyEnv2) MyMacbook:my-rep my-user$ which pip3
/usr/bin/pip3
Note - none of those paths match this one from my errors:
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
Also, here is the output from echo $PATH:
/Users/my-user/.nvm/versions/node/v8.14.1/bin
:/usr/local/bin
:/usr/bin
:/bin
:/usr/sbin
:/sbin
:/Library/Python/2.7/bin
:/Library/apache-maven-3.6.3/bin
:/usr/local/share/dotnet
:~/.dotnet/tools
:/Users/my-user/opt/miniconda3/envs/MyEnv2/bin
:/Users/my-user/opt/miniconda3/condabin
:/Users/my-user/google-cloud-sdk/bin
:/Users/my-user/.nvm/versions/node/v8.14.1/bin
:/Users/my-user/.nvm/versions/node/v8.14.1/bin
I resolved this by removing the following from: /etc/paths:
/Library/Python/2.7/bin
Once I had that line removed, I was able to see (nearly) identical output for which pip, which pip3, and which python. All 3 output the following (with the appropriate app at the end of the path):
/Users/my-user/opt/miniconda3/envs/MyEnv2/bin/python

Flask on PyCharm - ImportError: attempted relative import with no known parent package

I am trying to create and run a simple flask server on PyCharm community 2019.3
I work with python 3.7.
I used venv to create the project.
Flask was installed like this:
(venv) pip install flask
from the pycharm terminal on c:\Users\Damo\PycharmProjects\myflaskproj\venv\Lib\site-packages\flask\
In the Edit configuration I created a new python run configuration.
I used the above path as my script path and set "run" as the parameters.
I added FLASK_APP=myflaskproj.py to the environment parameters.
Finally I set C:\Users\Damo\PycharmProjects\myflaskproj as my working directory.
When I run the project, I get :
C:\Users\Damo\PycharmProjects\myflaskproj\venv\Scripts\python.exe C:/Users/Damo/PycharmProjects/myflaskproj/venv/Lib/site-packages/flask run
Traceback (most recent call last):
File "c:\Users\Damo\AppData\Local\Programs\Python\Python37-32\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\Users\Damo\AppData\Local\Programs\Python\Python37-32\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:/Users/Damo/PycharmProjects/myflaskproj/venv/Lib/site-packages/flask\__main__.py", line 13, in <module>
from .cli import main
ImportError: attempted relative import with no known parent package
Where do I go wrong?
Thanks
This is the complete answer...
Running flask server configuration for PyCharm

Categories