I am working on MacOS BigSur/Version 11.5.2/ Python 3.9.5.
So i keep getting this import mistake when i try to run my python code:
*import subprocess
import psutil
import schedule
import time
import csv
import sys
import httpx
import asyncio
import json
csv_headers = ["company_id", "dep_id", "host_id", "cpu_count", "cpu_load", "ram_total",
"ram_used", "diskspace_total", "diskspace_used", "diskspace_free", "operations_read",
"operations_write", "timeresponse_min_ms", "timeresponse_avg_ms", "timeresponse_max_ms"]
def get_statistics_windows():
statistics = {}
statistics['company_id'] = "comp301"
statistics['dep_id'] = "dep301"
statistics['host_id'] = "host301"
# Get Physical and Logical CPU Count
physical_and_logical_cpu_count = psutil.cpu_count()
statistics['cpu_count'] = physical_and_logical_cpu_count
# Get CPU load in percentage (system wide)
statistics['cpu_load'] = psutil.cpu_percent(interval=1)
# Memory usage
statistics['ram_total'] = round(psutil.virtual_memory().total / 1024 ** 3, 2)
statistics['ram_used'] = round(psutil.virtual_memory().used / 1024 ** 3, 2)
#Disk Usage
statistics['diskspace_total'] = round(psutil.disk_usage('/').total / 1024 ** 3, 2)
statistics['diskspace_used'] = round(psutil.disk_usage('/').used / 1024 ** 3, 2)
statistics['diskspace_free'] = round(psutil.disk_usage('/').free / 1024 ** 3, 2)
statistics['operations_read'] = psutil.disk_io_counters().read_count # read bytes
statistics['operations_write'] = psutil.disk_io_counters().write_count # written bytes
# Network latency
ping_result = subprocess.run(['ping', '-i 5', '-c 5', 'google.com'], stdout=subprocess.PIPE).stdout.decode(
'utf-8').split('\n')
min_response_time, max_response_time, avg_response_time = ping_result[0].split('=')[-1], ping_result[1].split('=')[-1], ping_result[2].split('=')[-1]
statistics['timeresponse_min_ms'] = min_response_time.replace('ms', '').strip()
statistics['timeresponse_avg_ms'] = avg_response_time.replace('ms', '').strip()
statistics['timeresponse_max_ms'] = max_response_time.replace('ms', '').strip()
return statistics*
There is 100% no mistake in the code because i was asked to test it, so it works on other machines.
I tried to do
pip install psutil
and got - "Successfully installed psutil-5.8.0" response but in my Visual Studio i still keep getting this error.
I also used a command sudo pip install --upgrade psutil which successfully update the package but still doesnt seem to work in my Visual Studio
Does anybody know how to make my Visual Studio see this package, so my code could run?
Thanks in advance!
Related
I am trying to execute this code in Visual Studio Code, the code works, my problem is related to the import of numpy; the other imports are working.
import codecs
from operator import le
import string
from struct import unpack
import paho.mqtt.client as mqtt
import struct
import numpy as np
def on_connect1(client1, userdata1, flags1, rc1):
client1.subscribe("MYTOPIC")
def on_message1(client1, userdata1, msg1):
#print(msg1.topic+" "+ "TERMORESISTENZA: "+str(msg1.payload))
Byte_Order = '<' # little-endian
Format_Characters = 'f' # float (4 bytes)
data_format = Byte_Order + Format_Characters
r = np.array(list(struct.iter_unpack(data_format, msg1.payload)), dtype=float)
print(r)
When I run the code it returns me this error:
ModuleNotFoundError: No module named 'numpy'
Any suggestions?
I used the Windows Command Prompt to install numpy with the command:
pip install numpy
and solved the problem
When I try running an example PyBullet file, like the one below, I keep getting the following error message:
import pybullet as p
from time import sleep
import pybullet_data
physicsClient = p.connect(p.GUI)
p.setAdditionalSearchPath(pybullet_data.getDataPath())
p.setGravity(0, 0, -10)
planeId = p.loadURDF("plane.urdf", [0,0,-2])
boxId = p.loadURDF("cube.urdf", [0,3,2],useMaximalCoordinates = True)
bunnyId = p.loadSoftBody("bunny.obj")#.obj")#.vtk")
useRealTimeSimulation = 1
if (useRealTimeSimulation):
p.setRealTimeSimulation(1)
p.changeDynamics(boxId,-1,mass=10)
while p.isConnected():
p.setGravity(0, 0, -10)
if (useRealTimeSimulation):
sleep(0.01) # Time in seconds.
else:
p.stepSimulation()
The error shows as following:
bunnyId = p.loadSoftBody("bunny.obj")#.obj")#.vtk")
error: Cannot load soft body.
I have Windows 10. I'm running PyBullet on a notebook (Python 3.6), but I get the same error with Visual Studio (Python 3.7). What can I do to fix it?
This is a solved issue in https://github.com/bulletphysics/bullet3/pull/4010#issue-1035353580,
either upgrade pybullet or copy the .obj file from the repository to pybullet_data directory will be fine.
I am trying to run a python file that works fine on my Windows machine on a remote server:
import collections
import pandas as pd
from pathlib import Path
import shelve
import cloudpickle
import numpy as np
import typing
from typing import List
import ray
from ray.rllib.agents import ppo
from ray.rllib.utils.spaces.space_utils import flatten_to_single_ndarray
from ray.tune import register_env
from ray.rllib.env.base_env import _DUMMY_AGENT_ID
from ray.rllib.policy.sample_batch import DEFAULT_POLICY_ID
.
.
.
if __name__ == '__main__':
WIN = False
ray.init(dashboard_port=8263)
daterange = pd.date_range('2017-01-01', periods=35040, freq='15T')
norming_factor = 10
actions_module = ActionsModuleContinuous()
batch_n_days = 1
kappa = 1000
seed = 1234
steps_per_episode = batch_n_days * 24 * 4
num_episodes = 5
device_config: List = list()
device_config.append(Device1(WIN=WIN, norming_factor=norming_factor, n_data_points=steps_per_episode))
device_config.append(Device2(WIN=WIN))
device_config.append(Device3(WIN=WIN))
It returns the following SyntaxError, for no apparent reason:
(venv) [<username>#<server> examples]$ python test.py
File "test.py", line 183
device_config: List = list()
^
SyntaxError: invalid syntax
I have tried deleting : List, which just had the effect of moving the same error to a seemingly arbitrary place further down the script. Any help is greatly appreciated.
device_config: List = list()
That syntax is adding type annotations for variable device_config.
If you get SyntaxError: invalid syntax exception, it means that your python interpreter used to run your code is not new enough. Nothing to do with your code, it just means that when you enter python in your shell, interpreter that gets executed is not new enough.
As part of the testing regime for building a python application [a Jupyter notebook, in this instance], I want to test that all the modules installed in the system will actually load.
import module from string variable pointed me to build the following code:
import re
import subprocess
s_counter = 0
f_counter = 0
errors = []
libraries = []
output = subprocess.run(['conda', 'list'], stdout=subprocess.PIPE).stdout.decode('utf-8').splitlines()
for line in output:
if not re.match('\s*#', line):
words = re.split('\s+', line)
if words[0] != 'python':
libraries.append(words[0])
for lib in libraries:
try:
lib_obj = __import__(lib)
globals()[lib] = lib_obj
s_counter += 1
except ImportError:
errors.append("ERROR: missing python library: " + lib)
f_counter += 1
print(f"Successfuly loaded {s_counter} libraries, failed to load {f_counter}")
print(f"{errors}")
The problem with this simplistic solution is that it assumes the module name is the same as the import name.... and often it isn't.
For example: .... install beautifulsoup4 and from bs4 import BeautifulSoup
So the question is, how do I find out the import string for a module... programatically?
I'm in a pickle with writing a script that can SSH into device, run a command and parse that data out to a file. I've written this using Pyparsing and Exscript then I found out that the device I'm going to be using this on is using Python 2.4.4 and Debian 4.1.1 so the modules will not work on this. Now I am back to the drawing board trying to find out how to do this with NO modules. Anyone have any reference or point me in the right direction for this? Thank you in advance.
Here is my code:
from Exscript.util.interact import read_login
from Exscript.protocols import SSH2
import uuid
from pyparsing import *
import re
import yaml
account = read_login()
conn = SSH2()
conn.connect('172.0.0.1')
conn.login(account)
conn.execute('foobar')
data = conn.response
conn.send('exit\r')
conn.close()
###### PARSER ######
date_regex = re.compile(r'\d\d-\d\d-\d\d')
time_regex = re.compile(r'\d\d:\d\d:\d\d')
pairs = [{'category': 'General Information',
'kv': Group(Word(alphanums) + Word(alphanums))},
{'category': 'Last Reset:',
'kv': Group(Word(alphas, max=1) + Word(alphas)) + Literal(':').suppress()
+ Group(Regex(date_regex) + Regex(time_regex)
+ Optional(SkipTo(LineEnd())))
}
]
# build list of categories with associated parsing rules
categories = [Word("# ").suppress() + x['category']
+ OneOrMore(Group(x['kv']))
for x in pairs]
# account for thing you don't have specific rules for
categories.append(Word("#").suppress() + Optional(SkipTo(LineEnd())) +
Group(OneOrMore(Combine(Word(alphanums) + SkipTo(LineEnd()))))
)
# OR all the categories together
categories_ored = categories[0]
for c in categories[1:]:
categories_ored |= c
configDef = OneOrMore(categories_ored)
suppress_tokens = ["show all", "SSH>", "Active System Configuration"]
suppresses = [Literal(x).suppress() for x in suppress_tokens]
for s in suppresses:
configDef.ignore(s)
result = configDef.parseString(data)
for e in result:
print(e)
with open('/Users/MyMac/development/data.yml', 'w') as outfile:
outfile.write( yaml.dump(e))
UPDATE
I have followed the advice below and now have Pexpect installed and found a older version of Python-Pyparsing that I have also installed. So I'm on my way again to getting my scripts to work with modules. Thanks!
Looks like this is already solved, but...
As long as your SSH is configured for this host (or the host doesn't require you to log-in), you should be able to do the following.
import os
""" This will execute foobar on the remote host
and store the command output to a text file
on your machine."""
os.system("ssh 172.0.0.1 foobar > ~/data.txt")
""" Commence processing """
data = open("data.txt", mode='r')
# and so on and so on
You can also use the subprocess library, but os.system for these types of tasks is the simplest IMO.