I am struggling to import "wmi" in visual studio code - python

i am using py 3.6 and whenever i type this:
import wmi
def avg(value_list):
num = 0
length = len(value_list)
for val in value_list:
num += val
return num/length
w = wmi.WMI(namespace=r"C:\Users\name\Desktop\OpenHardwareMonitor")
sensors = w.Sensor()
cpu_temps = []
gpu_temp = 0
for sensor in sensors:
if sensor.SensorType==u'Temperature' and not 'GPU' in sensor.Name:
cpu_temps += [float(sensor.Value)]
elif sensor.SensorType==u'Temperature' and 'GPU' in sensor.Name:
gpu_temp = sensor.Value
print("Avg CPU: {}").format(avg(cpu_temps))
print("GPU: {}").format(gpu_temp)
it comes out as:
PS C:\Users\name> & C:/Users/name/AppData/Local/Programs/Python/Python36/python.exe "c:/Users/name/gpu temps.py"
Traceback (most recent call last):
File "c:/Users/name/pip install gpiozero.py", line 1, in <module>
import wmi
ModuleNotFoundError: No module named 'wmi'
i went through alot of the options on the web and 1 other post and went through all the options, including " pywin32-300.win-amd64-py3.6.exe" on https://github.com/mhammond/pywin32/releases...
still doesnt work

In regards to the details given in the comments.
What I would generally suggest is to create virtual environments for each python project you have.
in visual studio code here is how you could do it:
Open your project folder with VS-Code
open the terminal inside VS-Code
execute C:\Users\name\AppData\Local\Programs\Python\Python36\python.exe -m venv env (or the path to your python installation you'd like to create a virtual environment of)
activate your enviornment by entering . .\env\Scripts\Activate.ps1 (assuming windows, you might need to enter Set-ExecutionPolicy Unrestricted -Scope Process to allow execution
now you should have a little (env) shown in the terminal, indicatung you're using the virtual environment.
next you can enter all the pip install commands you need for your project.

Related

Python not recognized in CMD

I am working in Windows 11, Python 3.10, Mu 1.1.0. I followed the instructions on ATBS to create a batch file to run a script from the WIN + R launcher. However in CMD the error A(below) pops up. I tried following instructions from the documentation on python.org, and in Appendix B of ATBS to edit environment variables. I added the following to PATH env variables: C:\Users\19139\MyPythonScript; C:\Users\19139\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.10\Python 3.10 (64-bit).lnk; and C:\Users\19139\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.10\Python 3.10 (64-bit).lnk\Scripts. Is there some issue with the fact that the PATH includes start menu? All I am able to call from CMD is py --version. When I try and directly call a python script I get Error B. I disabled the "App Installer"(s) under App Aliases for Python.exe and Python3.exe but no change in error. I can call py --version, but I am beyond the scope of my ability to figure out what to do. I am unsure of next possible step to problem solve, suggestions welcome.
Error A:
`enter code here`Python path configuration:
PYTHONHOME = (not set)
PYTHONPATH = (not set)
program name = 'C:\Users\19139\AppData\Local\Programs\Python\Python310\python.exe'
isolated = 0
environment = 1
user site = 1
import site = 1
sys._base_executable = 'C:\\Users\\19139\\AppData\\Local\\Programs\\Python\\Python310\\python.exe'
sys.base_prefix = ''
sys.base_exec_prefix = ''
sys.platlibdir = 'lib'
sys.executable = 'C:\\Users\\19139\\AppData\\Local\\Programs\\Python\\Python310\\python.exe'
sys.prefix = ''
sys.exec_prefix = ''
sys.path = [
'C:\\Users\\19139\\AppData\\Local\\Programs\\Python\\Python310\\python310.zip',
'C:\\Users\\19139\\AppData\\Local\\Programs\\Python\\Python310\\Lib\\',
'C:\\Users\\19139\\AppData\\Local\\Programs\\Python\\Python310\\DLLs\\',
'C:\\Users\\19139\\AppData\\Local\\Programs\\Python\\Python310',
]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
Traceback (most recent call last):
File "C:\Users\19139\AppData\Local\Programs\Python\Python310\Lib\encodings\__init__.py", line 31, in <module>
ModuleNotFoundError: No module named 'codecs'
Error B
C:\Users\19139>python "C:\Users\19139\MyPythonScript\mClip.py"
Python was not found; run without arguments to install from the Microsoft Store, or
disable this shortcut from Settings > Manage App Execution Aliases.
First of all, unlike ubuntu/ linux Windows doesn't detect python on its own. That's why we need to specify the path manually, to make it aware of python.
Is there some issue with the fact that the PATH includes start menu?
Yes, there is issue there. PATH for python doesn't include Start Menu generally.
You are looking at the wrong place or have installed python package at the wrong location. Find the correct python package location.
Python PATH looks something like this:
C:\Users\user_name\python_installerpkg_name
For example, if you have installed python using a python installer pkg like anaconda/ mini-conda, you can use its command prompt to find the location via where python. Then use the location to set the PATH in environment variables. the PATH would look like: C:\Users\user_name\anaconda
Error B
Until you configure the correct path in the environment variables you won't be able to use the command python filename.py from the command line.

Unable to access packages inside virtual environment on startup

I am working on creating a bash file on my raspberry pi in order to be able to launch a webapp project that I have made when the raspberry pi starts up. I have been able to figure out most of it, except for I have issues with the virtual environment letting me access my packages that were installed in the virtual environment. When I run
workon tm
python main.py
in the terminal I am able to get my project working fine. But when I run my bash file
#!/bin/bash
# startup.sh
#Open Chromium on the Raspberry Pi on fullscreen on bootup. Then open the virtual environemnt, and then run our python script
#su - pi -c "/usr/bin/chromium-browser --start-fullscreen 127.0.0.1:5000"
cd /
cd /home/pi/MySQLAppTrueTrue
source /usr/local/bin/virtualenvwrapper.sh
source ~/.bashrc
#workon /home/pi/.virtualenvs/tm
workon tm
sudo python /home/pi/MySQLAppTrueTrue/main.py
I get the error
pi#raspberrypi:~/MySQLAppTrueTrue $ ./startup.sh
Traceback (most recent call last):
File "/home/pi/MySQLAppTrueTrue/main.py", line 1, in <module>
from webapp import app #import the web app
File "/home/pi/MySQLAppTrueTrue/webapp/__init__.py", line 2, in <module>
from flask_mysqldb import MySQL #import mySQL
ImportError: No module named flask_mysqldb
Does this mean that my workon tm line isnt actually causing the device to be working on the virtual environment. If so how do I get that to work? Or do I just need to install my packages globally instead of in the virtual environment?
Instead of all that virtualenvwrapper workon magic, you should probably just use that virtualenv's Python interpreter:
#!/bin/bash
cd /home/pi/MySQLAppTrueTrue
sudo /home/pi/.virtualenvs/tm/bin/python /home/pi/MySQLAppTrueTrue/main.py

Problem with accessing virtual environment Python from R Markdown

Note: I'm on Windows using Git Bash.
So, I am trying to setup a dev environment for the work for my class. It is going to involve a combination of coding in R and Python.
I created virtual environments using pipenv and virtualenv and ran into the same problem with both. So, first, let's create a virtual environment for the project in a sub-folder dev_env:
cd project_folder/dev_env
pipenv --python 3.7
pipenv --py
Output
C:\Users\Ra Me\.virtualenvs\dev_env-5TUtSZI9\Scripts\python.exe
Now I'm going into my file.rmd and trying the reticulate package.
#install.packages("reticulate")
library(reticulate)
Next, I tried 2 methods:
Sys.setenv(RETICULATE_PYTHON = "C:/Users/Ra Me/.virtualenvs/dev_env-5TUtSZI9/Scripts")
or
use_virtualenv("C:/Users/Ra Me/.virtualenvs/dev_env-5TUtSZI9/", required = TRUE)
x = 1
if x:
print('Hello!')
Both of them produced the error
Fatal Python error: initfsencoding: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'
However, when I change the path to the Python environment that's installed for all users on my machine, it works.
Sys.setenv(RETICULATE_PYTHON = "C:/Program Files/Python37/")
This method also works. However, here we are not even using the reticulate project.
knitr::opts_chunk$set(engine.path = list(
python = "C:/Program Files/Python37/python.exe",
r = "C:/Program Files/R/R-3.6.1/bin/R.exe"
))

Can't Import Modules Python 3 Windows

Problem Description: When running scripts from the terminal (example: navigate to C:\PythonPrograms\ProjectName> and input file name so it looks like C:\PythonPrograms\ProjectName>main.py), the script fails to run and an error message is received. For example, if a script includes import numpy, the error is ImportError: No module named numpy. When running from an IDE such as spyder, modules import correctly.
Modules were installed with Anaconda, and appear under C:\Python37\Lib\site-packages
Systems: Windows 10, Python 3.7.0
Code examples
This script successfully runs:
x = 1
for i in range(10):
print x
x += 1
C:\Python Programs>test.py
1
2
3
4
5
6
7
8
9
10
This script does not run:
import serial
x = 1
for i in range(10):
print x
x += 1
C:\Python Programs>test.py
Traceback (most recent call last):
File "C:\Python Programs\test.py", line 1, in <module>
import serial
ImportError: No module named serial
Edit:
Python is added to the system path as shown:Screenclip
Solved!
Checked the path, it was all correct. Went to uninstall python and reinstall, discovered that there was an unknown python 2.7 installed, once it was uninstalled everything worked correctly.
You have to make sure that your Python Path Environment Variable is correctly set on your system.
This should look like
C:\Users\YOUR_USERNAME\AppData\Local\Programs\Python\Python37-32\
If it don't, try to reinstall Python and check Add Python 3.7 to PATH

Why is $PATH in remote deployment path different from $PATH in remote system?

I'm currently working on Pycharm with remote python Interpreter(miniconda3/bin/python).
So when I type echo $PATH in remote server, it prints
/home/woosung/bin:/home/woosung/.local/bin:/home/woosung/miniconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
I created project in Pycharm and set remote python Interpreter as miniconda3 python, it works well when I just run some *.py files.
But when I typed some os.system() lines, weird things happened.
For instance, in test.py from Pycharm project
import os
os.system('echo $PATH')
os.system('python --version')
Output is
ssh://woosung#xxx.xxx.xxx.xxx:xx/home/woosung/miniconda3/bin/python -u /tmp/pycharm_project_203/test.py
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
Python 2.7.12
Process finished with exit code 0
I tried same command in remote server,
woosung#test-pc:~$ echo $PATH
/home/woosung/bin:/home/woosung/.local/bin:/home/woosung/miniconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
woosung#test-pc:~$ python --version
Python 3.6.6 :: Anaconda, Inc.
PATH and the version of python are totally different! How can I fix this?
I've already tried add os.system('export PATH="$PATH:$HOME/miniconda3/bin"') to test.py. But it still gives same $PATH.(/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games)
EDIT
Thanks to the comment of #Dietrich Epp, I successfully add interpreter path to the shell $PATH.
(os.environ["PATH"] += ":/home/woosung/miniconda3/bin")
But I stuck the more basic problem. When I add the path and execute command the some *.py file including import library which is only in miniconda3, the shell gives ImportError.
For instance, in test.py
import matplotlib
os.environ["PATH"] += ":/home/woosung/miniconda3/bin"
os.system("python import_test.py")
and import_test.py
import matplotlib
And when I run test.py,
Traceback (most recent call last):
File "import_test.py", line 1, in <module>
import matplotlib
ImportError: No module named matplotlib
Looks like the shell doesn't understand how to utilize modified $PATH.
I find the solution.
It is not direct but quite simple.
I changed os.system("python import_test.py") to os.system(sys.executable + ' import_test.py').
This makes the shell uses the Pycharm remote interpreter(miniconda3), not original.

Categories