skip to navigation
skip to content

Planet Python

Last update: August 08, 2022 01:41 AM UTC

August 07, 2022


ItsMyCode

Python Dictionary get()

Python Dictionary get() is a built-in function that returns the value for a specified key if present in the dictionary. If the key is not present, it returns None by default. 

In this tutorial, we will learn about the Python Dictionary get() method with the help of examples.

Syntax of Dictionary get() 

The syntax of the get() method is:

dict.get(key,value) 

get() Parameters

The get() method takes two parameters.

Return Value from Dictionary get()

The get() method returns 

Example 1 – How to use get() method in Dictionary

company = {"name": "Microsoft", "location": "Seattle"}

print("Company Name:", company.get("name"))
print("Company Location:", company.get("location"))

# In case of wrong key name without default value
print("Company Headquarters:", company.get("headquarters"))

# In case of wrong key name with default value
print("Company Headquarters:", company.get("headquarters","Not Found"))

Output

Company Name: Microsoft
Company Location: Seattle      
Company Headquarters: None     
Company Headquarters: Not Found

Example 2 – Difference between get() method and dict[key] to access elements

The get() method will not raise any exception if the key is not found, it either returns None or a default value if provided.

On the other hand when we use dict[key] if the key is not found KeyError exception is raised.

company = {"name": "Microsoft", "location": "Seattle"}

# In case of wrong key name without default value
print("Company Headquarters:", company.get("headquarters"))

# In case of wrong key name with default value
print("Company Headquarters:", company.get("headquarters","Not Found"))

# when used dict[key]
print(company["headquarters"])

Output

Company Headquarters: None
Company Headquarters: Not Found
Traceback (most recent call last):
  File "C:\Personal\IJS\Python_Samples\program.py", line 9, in <module>
    print(company["headquarters"])
KeyError: 'headquarters'

August 07, 2022 06:21 PM UTC

ModuleNotFoundError: No module named ‘six’

In Python, ModuleNotFoundError: No module named ‘six’ error occurs if we try to import the ‘six‘ module without installing the package or if you have not installed it in the correct environment.

In this tutorial, let’s look at installing the six module correctly in different operating systems and solve ModuleNotFoundError: No module named ‘six’ error.  

What is ModuleNotFoundError: No module named ‘six’?

There are various reasons why we get the ModuleNotFoundError: No module named ‘six’ error

If you are getting an error installing pip, checkout pip: command not found to resolve the issue.

How to fix ModuleNotFoundError: No module named ‘six’?

six is not a built-in module (it doesn’t come with the default python installation) in Python; you need to install it explicitly using the pip installer and then use it.  

Six is a Python 2 and 3 compatibility library. It provides utility functions for smoothing over the differences between the Python versions with the goal of writing Python code that is compatible on both Python versions

We can fix the error by installing the ‘six‘ module by running the pip install six command in your terminal/shell.

We can verify if the package is installed correctly by running the following command in the terminal/shell.

This will provide the details of the package installed, including the version number, license, and the path it is installed. If the module is not installed, you will get a warning message in the terminal stating WARNING: Package(s) not found: six.

pip show six

Output

Name: six
Version: 1.16.0
Summary: Python 2 and 3 compatibility utilities
Home-page: https://github.com/benjaminp/six    
Author: Benjamin Peterson
Author-email: benjamin@python.org
License: MIT
Location: c:\personal\ijs\python_samples\venv\lib\site-packages
Requires:
Required-by: tensorflow, python-dateutil, Keras-Preprocessing, grpcio, google-pasta, google-auth, Flask-RESTful, Flask-Cors, astunparse, asttokens

Solution 1 – Installing and using the six module in a proper way

Based on the Python version and the operating system you are running, run the relevant command to install the six module.

# If you are using Python 2 (Windows)
pip install six

# if you are using Python 3 (Windows)
pip3 install six

# If the pip is not set as environment varibale PATH
python -m pip install six

# If you are using Python 2 (Linux)
sudo pip install six

# if you are using Python 3 (Linux)
sudo pip3 install six

# In case if you have to easy_install
sudo easy_install -U six

# On Centos
yum install six

# On Ubuntu
sudo apt-get install six

# If you are installing it in Anaconda 
conda install -c conda-forge six

Once you have installed the six module, we can now import it inside our code and use it as shown below.

from six import get_unbound_function

class X(object):
    def method(self):
        pass
method_function = get_unbound_function(X.method)

Solution 2 – Verify if the IDE is set to use the correct Python version

If you are still getting the same error even after installing the package, you can verify if the IDE you are using is configured with the correct version of the Python interpreter.

For Eg:- In the case of Visual Studio Code, we can set the Python version by pressing CTRL + Shift + Por ( + Shift + P on Mac) to open the command palette.

Once the command palette opens, select the Python interpreter and select the correct version of Python and also the virtual environment(if configured) as shown below.

image 1

Python Interpreter

Solution 3 – Installing six inside the virtual environment

Many different IDEs like Jupyter Notebook, Spyder, Anaconda, or PyCharm often install their own virtual environment of Python to keep things clean and separated from your global Python.

If you are using VS Code, then you can also create a virtual environment, as shown below.

In the case of virtual environments, you need to ensure that the six module needs to be installed inside the virtual environment and not globally.

Step 1: Create a Virtual Environment. If you have already created a virtual environment, then proceed to step 2.

Step 2: Activate the Virtual Environment

Step 3: Install the required module using the pip install command

# Create a virtual Environment
py -3 -m venv venv

# Activate the virtual environment (windows command)
venv\Scripts\activate.bat

# Activate the virtual environment (windows powershell)
venv\Scripts\Activate.ps1

# Activate the virtual environment (Linux)
source venv/bin/activate

# Install six inside the virtual environment
pip install six

Solution 4 – Ensure that a module name is not declared name a variable name.

Last but not least, you may need to cross-check and ensure that you haven’t declared a variable with the same name as the module name.

You should check if you haven’t named any files as six.py as it may shadow the original six module.

If the issue is still not solved, you can try removing the package and installing it once again, restart the IDE, and check the paths to ensure that packages are installed in the correct environment path and Python version.

Conclusion

The ModuleNotFoundError: No module named ‘six’ error occurs when we try to import the ‘six‘ module without installing the package or if you have not installed it in the correct environment.

We can resolve the issue by installing the six module by running the pip install six command. Also, ensure that the module is installed in the proper environment in case you use any virtual environments, and the Python version is appropriately set in the IDE that you are running the code.

August 07, 2022 04:21 PM UTC

ModuleNotFoundError: No module named ‘yaml’

In Python, ModuleNotFoundError: No module named ‘yaml’ error occurs if we try to import the ‘pyyaml‘ module without installing the package or if you have not installed it in the correct environment.

In this tutorial, let’s look at installing the pyyaml module correctly in different operating systems and solve ModuleNotFoundError: No module named ‘yaml’ error.  

What is ModuleNotFoundError: No module named ‘yaml’?

There are various reasons why we get the ModuleNotFoundError: No module named ‘yaml’ error

If you are getting an error installing pip, checkout pip: command not found to resolve the issue.

How to fix ModuleNotFoundError: No module named ‘yaml’?

pyyaml is not a built-in module (it doesn’t come with the default python installation) in Python; you need to install it explicitly using the pip installer and then use it.  

YAML is a data serialization format designed for human readability and interaction with scripting languages. PyYAML is a YAML parser and emitter for Python.

PyYAML features a complete YAML 1.1 parser, Unicode support, pickle support, capable extension API, and sensible error messages. PyYAML supports standard YAML tags and provides Python-specific tags that allow representing an arbitrary Python object.

We can fix the error by installing the ‘pyyaml‘ module by running the pip install pyyaml command in your terminal/shell.

We can verify if the package is installed correctly by running the following command in the terminal/shell.

This will provide the details of the package installed, including the version number, license, and the path it is installed. If the module is not installed, you will get a warning message in the terminal stating WARNING: Package(s) not found: pyyaml.

pip show pyyaml

Output

Name: PyYAML
Version: 6.0
Summary: YAML parser and emitter for Python
Home-page: https://pyyaml.org/
Author: Kirill Simonov
Author-email: xi@resolvent.net
License: MIT
Location: c:\personal\ijs\python_samples\venv\lib\site-packages
Requires:
Required-by: Naked

Solution 1 – Installing and using the pyyaml module in a proper way

Based on the Python version and the operating system you are running, run the relevant command to install the pyyaml module.

# If you are using Python 2 (Windows)
pip install pyyaml

# if you are using Python 3 (Windows)
pip3 install pyyaml

# If the pip is not set as environment varibale PATH
python -m pip install pyyaml

# If you are using Python 2 (Linux)
sudo pip install pyyaml

# if you are using Python 3 (Linux)
sudo pip3 install pyyaml

# In case if you have to easy_install
sudo easy_install -U pyyaml

# On Centos
yum install pyyaml

# On Ubuntu
sudo apt-get install pyyaml

# If you are installing it in Anaconda 
conda install -c conda-forge pyyaml

Once you have installed the pyyaml module, we can now import it inside our code and use it as shown below.

import yaml
with open('config.yml', 'r') as file:
    config = yaml.safe_load(file)
    print(config)

Output

version: 2.1

# Define the jobs we want to run for this project
jobs:
  build:
    docker:
      - image: cimg/<language>:<version TAG>
        auth:
          username: mydockerhub-user
          password: $DOCKERHUB_PASSWORD # context / project UI env-var reference
    steps:
      - checkout
      - run: echo "this is the build job"
 
# Orchestrate our job run sequence
workflows:
  build_and_test:
    jobs:
      - build
      - test:
          requires:
            - build

Solution 2 – Verify if the IDE is set to use the correct Python version

If you are still getting the same error even after installing the package, you can verify if the IDE you are using is configured with the correct version of the Python interpreter.

For Eg:- In the case of Visual Studio Code, we can set the Python version by pressing CTRL + Shift + Por ( + Shift + P on Mac) to open the command palette.

Once the command palette opens, select the Python interpreter and select the correct version of Python and also the virtual environment(if configured) as shown below.

image 1

Python Interpreter

Solution 3 – Installing pyyaml inside the virtual environment

Many different IDEs like Jupyter Notebook, Spyder, Anaconda, or PyCharm often install their own virtual environment of Python to keep things clean and separated from your global Python.

If you are using VS Code, then you can also create a virtual environment, as shown below.

In the case of virtual environments, you need to ensure that the pyyaml module needs to be installed inside the virtual environment and not globally.

Step 1: Create a Virtual Environment. If you have already created a virtual environment, then proceed to step 2.

Step 2: Activate the Virtual Environment

Step 3: Install the required module using the pip install command

# Create a virtual Environment
py -3 -m venv venv

# Activate the virtual environment (windows command)
venv\Scripts\activate.bat

# Activate the virtual environment (windows powershell)
venv\Scripts\Activate.ps1

# Activate the virtual environment (Linux)
source venv/bin/activate

# Install pyyaml inside the virtual environment
pip install pyyaml

Solution 4 – Ensure that a module name is not declared name a variable name.

Last but not least, you may need to cross-check and ensure that you haven’t declared a variable with the same name as the module name.

You should check if you haven’t named any files as yaml.py as it may shadow the original pyyaml module.

If the issue is still not solved, you can try removing the package and installing it once again, restart the IDE, and check the paths to ensure that packages are installed in the correct environment path and Python version.

Conclusion

The ModuleNotFoundError: No module named ‘yaml’ error occurs when we try to import the ‘pyyaml‘ module without installing the package or if you have not installed it in the correct environment.

We can resolve the issue by installing the pyyaml module by running the pip install pyyaml command. Also, ensure that the module is installed in the proper environment in case you use any virtual environments, and the Python version is appropriately set in the IDE that you are running the code.

August 07, 2022 11:08 AM UTC

ModuleNotFoundError: No module named ‘xlsxwriter’

In Python, ModuleNotFoundError: No module named ‘xlsxwriter’ error occurs if we try to import the ‘xlsxwriter‘ module without installing the package or if you have not installed it in the correct environment.

In this tutorial, let’s look at installing the xlsxwriter module correctly in different operating systems and solve ModuleNotFoundError: No module named ‘xlsxwriter’ error.  

What is ModuleNotFoundError: No module named ‘xlsxwriter’?

There are various reasons why we get the ModuleNotFoundError: No module named ‘xlsxwriter’ error

If you are getting an error installing pip, checkout pip: command not found to resolve the issue.

How to fix ModuleNotFoundError: No module named ‘xlsxwriter’?

xlsxwriter is not a built-in module (it doesn’t come with the default python installation) in Python; you need to install it explicitly using the pip installer and then use it.  

XlsxWriter is a Python module for writing files in the Excel 2007+ XLSX file format. XlsxWriter can be used to write text, numbers, formulas, and hyperlinks to multiple worksheets and it supports features such as formatting and many more.

We can fix the error by installing the ‘xlsxwriter‘ module by running the pip install xlsxwriter command in your terminal/shell.

We can verify if the package is installed correctly by running the following command in the terminal/shell.

This will provide the details of the package installed, including the version number, license, and the path it is installed. If the module is not installed, you will get a warning message in the terminal stating WARNING: Package(s) not found: xlsxwriter.

pip show xlsxwriter

Output

Name: XlsxWriter
Version: 3.0.3
Summary: A Python module for creating Excel XLSX files.
Home-page: https://github.com/jmcnamara/XlsxWriter
Author: John McNamara
Author-email: jmcnamara@cpan.org
License: BSD-2-Clause
Location: c:\personal\ijs\python_samples\venv\lib\site-packages

Solution 1 – Installing and using the xlsxwriter module in a proper way

Based on the Python version and the operating system you are running, run the relevant command to install the xlsxwriter module.

# If you are using Python 2 (Windows)
pip install xlsxwriter

# if you are using Python 3 (Windows)
pip3 install xlsxwriter

# If the pip is not set as environment varibale PATH
python -m pip install xlsxwriter

# If you are using Python 2 (Linux)
sudo pip install xlsxwriter

# if you are using Python 3 (Linux)
sudo pip3 install xlsxwriter

# In case if you have to easy_install
sudo easy_install -U xlsxwriter

# On Centos
yum install xlsxwriter

# On Ubuntu
sudo apt-get install xlsxwriter

# If you are installing it in Anaconda 
conda install -c conda-forge xlsxwriter

Once you have installed the xlsxwriter module, we can now import it inside our code and use it as shown below.

import xlsxwriter

# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook('Expenses01.xlsx')
worksheet = workbook.add_worksheet()

# Some data we want to write to the worksheet.
expenses = (
    ['Rent', 1000],
    ['Gas',   100],
    ['Food',  300],
    ['Gym',    50],
)

# Start from the first cell. Rows and columns are zero indexed.
row = 0
col = 0

# Iterate over the data and write it out row by row.
for item, cost in (expenses):
    worksheet.write(row, col,     item)
    worksheet.write(row, col + 1, cost)
    row += 1

# Write a total using a formula.
worksheet.write(row, 0, 'Total')
worksheet.write(row, 1, '=SUM(B1:B4)')

workbook.close()

Solution 2 – Verify if the IDE is set to use the correct Python version

If you are still getting the same error even after installing the package, you can verify if the IDE you are using is configured with the correct version of the Python interpreter.

For Eg:- In the case of Visual Studio Code, we can set the Python version by pressing CTRL + Shift + Por ( + Shift + P on Mac) to open the command palette.

Once the command palette opens, select the Python interpreter and select the correct version of Python and also the virtual environment(if configured) as shown below.

image 1

Python Interpreter

Solution 3 – Installing xlsxwriter inside the virtual environment

Many different IDEs like Jupyter Notebook, Spyder, Anaconda, or PyCharm often install their own virtual environment of Python to keep things clean and separated from your global Python.

If you are using VS Code, then you can also create a virtual environment, as shown below.

In the case of virtual environments, you need to ensure that the xlsxwriter module needs to be installed inside the virtual environment and not globally.

Step 1: Create a Virtual Environment. If you have already created a virtual environment, then proceed to step 2.

Step 2: Activate the Virtual Environment

Step 3: Install the required module using the pip install command

# Create a virtual Environment
py -3 -m venv venv

# Activate the virtual environment (windows command)
venv\Scripts\activate.bat

# Activate the virtual environment (windows powershell)
venv\Scripts\Activate.ps1

# Activate the virtual environment (Linux)
source venv/bin/activate

# Install xlsxwriter inside the virtual environment
pip install xlsxwriter

Solution 4 – Ensure that a module name is not declared name a variable name.

Last but not least, you may need to cross-check and ensure that you haven’t declared a variable with the same name as the module name.

You should check if you haven’t named any files as xlsxwriter.py as it may shadow the original xlsxwriter module.

If the issue is still not solved, you can try removing the package and installing it once again, restart the IDE, and check the paths to ensure that packages are installed in the correct environment path and Python version.

Conclusion

The ModuleNotFoundError: No module named ‘xlsxwriter’ error occurs when we try to import the ‘xlsxwriter‘ module without installing the package or if you have not installed it in the correct environment.

We can resolve the issue by installing the xlsxwriter module by running the pip install xlsxwriter command. Also, ensure that the module is installed in the proper environment in case you use any virtual environments, and the Python version is appropriately set in the IDE that you are running the code.

August 07, 2022 11:08 AM UTC

ModuleNotFoundError: No module named ‘xgboost’

In Python, ModuleNotFoundError: No module named ‘xgboost’ error occurs if we try to import the ‘xgboost‘ module without installing the package or if you have not installed it in the correct environment.

In this tutorial, let’s look at installing the xgboost module correctly in different operating systems and solve ModuleNotFoundError: No module named ‘xgboost’ error.  

What is ModuleNotFoundError: No module named ‘xgboost’?

There are various reasons why we get the ModuleNotFoundError: No module named ‘xgboost’ error

If you are getting an error installing pip, checkout pip: command not found to resolve the issue.

How to fix ModuleNotFoundError: No module named ‘xgboost’?

xgboost is not a built-in module (it doesn’t come with the default python installation) in Python; you need to install it explicitly using the pip installer and then use it.  

XGBoost is an optimized distributed gradient boosting library designed to be highly efficientflexible, and portable. It implements machine learning algorithms under the Gradient Boosting framework. XGBoost provides a parallel tree boosting (also known as GBDT, GBM) that solves many data science problems in a fast and accurate way. The same code runs on a major distributed environment (Hadoop, SGE, MPI) and can solve problems beyond billions of examples.

We can fix the error by installing the ‘xgboost‘ module by running the pip install xgboost command in your terminal/shell.

We can verify if the package is installed correctly by running the following command in the terminal/shell.

This will provide the details of the package installed, including the version number, license, and the path it is installed. If the module is not installed, you will get a warning message in the terminal stating WARNING: Package(s) not found: xgboost.

pip show xgboost

Output

Name: xgboost
Version: 1.6.1
Summary: XGBoost Python Package
Home-page: https://github.com/dmlc/xgboost
Author:
Author-email:
License: Apache-2.0
Location: c:\personal\ijs\python_samples\venv\lib\site-packages
Requires: scipy, numpy

Solution 1 – Installing and using the xgboost module in a proper way

Based on the Python version and the operating system you are running, run the relevant command to install the xgboost module.

# If you are using Python 2 (Windows)
pip install xgboost

# if you are using Python 3 (Windows)
pip3 install xgboost

# If the pip is not set as environment varibale PATH
python -m pip install xgboost

# If you are using Python 2 (Linux)
sudo pip install xgboost

# if you are using Python 3 (Linux)
sudo pip3 install xgboost

# In case if you have to easy_install
sudo easy_install -U xgboost

# On Centos
yum install xgboost

# On Ubuntu
sudo apt-get install xgboost

# If you are installing it in Anaconda 
conda install -c conda-forge xgboost

Once you have installed the xgboost module, we can now import it inside our code and use it as shown below.

# First XGBoost model for Pima Indians dataset
from numpy import loadtxt
from xgboost import XGBClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# load data
dataset = loadtxt("pima-indians-diabetes.csv", delimiter=",")

# split data into X and y
X = dataset[:, 0:8]
Y = dataset[:, 8]

# split data into train and test sets
seed = 7
test_size = 0.33
X_train, X_test, y_train, y_test = train_test_split(
    X, Y, test_size=test_size, random_state=seed
)

# fit model no training data
model = XGBClassifier()
model.fit(X_train, y_train)

# make predictions for test data
y_pred = model.predict(X_test)
predictions = [round(value) for value in y_pred]

# evaluate predictions
accuracy = accuracy_score(y_test, predictions)
print("Accuracy: %.2f%%" % (accuracy * 100.0))

Output

Accuracy: 82.15%

Solution 2 – Verify if the IDE is set to use the correct Python version

If you are still getting the same error even after installing the package, you can verify if the IDE you are using is configured with the correct version of the Python interpreter.

For Eg:- In the case of Visual Studio Code, we can set the Python version by pressing CTRL + Shift + Por ( + Shift + P on Mac) to open the command palette.

Once the command palette opens, select the Python interpreter and select the correct version of Python and also the virtual environment(if configured) as shown below.

image 1

Python Interpreter

Solution 3 – Installing xgboost inside the virtual environment

Many different IDEs like Jupyter Notebook, Spyder, Anaconda, or PyCharm often install their own virtual environment of Python to keep things clean and separated from your global Python.

If you are using VS Code, then you can also create a virtual environment, as shown below.

In the case of virtual environments, you need to ensure that the xgboost module needs to be installed inside the virtual environment and not globally.

Step 1: Create a Virtual Environment. If you have already created a virtual environment, then proceed to step 2.

Step 2: Activate the Virtual Environment

Step 3: Install the required module using the pip install command

# Create a virtual Environment
py -3 -m venv venv

# Activate the virtual environment (windows command)
venv\Scripts\activate.bat

# Activate the virtual environment (windows powershell)
venv\Scripts\Activate.ps1

# Activate the virtual environment (Linux)
source venv/bin/activate

# Install xgboost inside the virtual environment
pip install xgboost

Solution 4 – Ensure that a module name is not declared name a variable name.

Last but not least, you may need to cross-check and ensure that you haven’t declared a variable with the same name as the module name.

You should check if you haven’t named any files as xgboost.py as it may shadow the original xgboost module.

If the issue is still not solved, you can try removing the package and installing it once again, restart the IDE, and check the paths to ensure that packages are installed in the correct environment path and Python version.

Conclusion

The ModuleNotFoundError: No module named ‘xgboost’ error occurs when we try to import the ‘xgboost‘ module without installing the package or if you have not installed it in the correct environment.

We can resolve the issue by installing the xgboost module by running the pip install xgboost command. Also, ensure that the module is installed in the proper environment in case you use any virtual environments, and the Python version is appropriately set in the IDE that you are running the code.

August 07, 2022 11:08 AM UTC

ModuleNotFoundError: No module named ‘virtualenv’

In Python, ModuleNotFoundError: No module named ‘virtualenv’ error occurs if we try to import the ‘virtualenv‘ module without installing the package or if you have not installed it in the correct environment.

In this tutorial, let’s look at installing the virtualenv module correctly in different operating systems and solve ModuleNotFoundError: No module named ‘virtualenv’ error.  

What is ModuleNotFoundError: No module named ‘virtualenv’?

There are various reasons why we get the ModuleNotFoundError: No module named ‘virtualenv’ error

If you are getting an error installing pip, checkout pip: command not found to resolve the issue.

How to fix ModuleNotFoundError: No module named ‘virtualenv’?

virtualenv is not a built-in module (it doesn’t come with the default python installation) in Python; you need to install it explicitly using the pip installer and then use it.  

virtualenv is a tool to create isolated Python environments. Since Python 3.3, a subset of it has been integrated into the standard library under the venv module. The venv module does not offer all features of this library, to name just a few more prominent:

We can fix the error by installing the ‘virtualenv‘ module by running the pip install virtualenv command in your terminal/shell.

We can verify if the package is installed correctly by running the following command in the terminal/shell.

This will provide the details of the package installed, including the version number, license, and the path it is installed. If the module is not installed, you will get a warning message in the terminal stating WARNING: Package(s) not found: virtualenv.

pip show virtualenv

Output

Name: virtualenv
Version: 8.1.3
Summary: Composable command line interface toolkit
Home-page: https://palletsprojects.com/p/virtualenv/   
Author: Armin Ronacher
Author-email: armin.ronacher@active-4.com
License: BSD-3-Clause
Location: c:\personal\ijs\python_samples\venv\lib\site-packages
Requires: colorama
Required-by: black

Solution 1 – Installing and using the virtualenv module in a proper way

Based on the Python version and the operating system you are running, run the relevant command to install the virtualenv module.

# If you are using Python 2 (Windows)
pip install virtualenv

# if you are using Python 3 (Windows)
pip3 install virtualenv

# If the pip is not set as environment varibale PATH
python -m pip install virtualenv

# If you are using Python 2 (Linux)
sudo pip install virtualenv

# if you are using Python 3 (Linux)
sudo pip3 install virtualenv

# In case if you have to easy_install
sudo easy_install -U virtualenv

# On Centos
yum install virtualenv

# On Ubuntu
sudo apt-get install virtualenv

# If you are installing it in Anaconda 
conda install -c anaconda virtualenv

Solution 2 – Verify if the IDE is set to use the correct Python version

If you are still getting the same error even after installing the package, you can verify if the IDE you are using is configured with the correct version of the Python interpreter.

For Eg:- In the case of Visual Studio Code, we can set the Python version by pressing CTRL + Shift + Por ( + Shift + P on Mac) to open the command palette.

Once the command palette opens, select the Python interpreter and select the correct version of Python and also the virtual environment(if configured) as shown below.

image 1

Python Interpreter

Solution 3 – Installing virtualenv inside the virtual environment

Many different IDEs like Jupyter Notebook, Spyder, Anaconda, or PyCharm often install their own virtual environment of Python to keep things clean and separated from your global Python.

If you are using VS Code, then you can also create a virtual environment, as shown below.

In the case of virtual environments, you need to ensure that the virtualenv module needs to be installed inside the virtual environment and not globally.

Step 1: Create a Virtual Environment. If you have already created a virtual environment, then proceed to step 2.

Step 2: Activate the Virtual Environment

Step 3: Install the required module using the pip install command

# Create a virtual Environment
py -3 -m venv venv

# Activate the virtual environment (windows command)
venv\Scripts\activate.bat

# Activate the virtual environment (windows powershell)
venv\Scripts\Activate.ps1

# Activate the virtual environment (Linux)
source venv/bin/activate

# Install virtualenv inside the virtual environment
pip install virtualenv

Solution 4 – Ensure that a module name is not declared name a variable name.

Last but not least, you may need to cross-check and ensure that you haven’t declared a variable with the same name as the module name.

You should check if you haven’t named any files as virtualenv.py as it may shadow the original virtualenv module.

If the issue is still not solved, you can try removing the package and installing it once again, restart the IDE, and check the paths to ensure that packages are installed in the correct environment path and Python version.

Conclusion

The ModuleNotFoundError: No module named ‘virtualenv’ error occurs when we try to import the ‘virtualenv‘ module without installing the package or if you have not installed it in the correct environment.

We can resolve the issue by installing the virtualenv module by running the pip install virtualenv command. Also, ensure that the module is installed in the proper environment in case you use any virtual environments, and the Python version is appropriately set in the IDE that you are running the code.

August 07, 2022 11:07 AM UTC

ModuleNotFoundError: No module named ‘urllib3’

In Python, ModuleNotFoundError: No module named ‘urllib3’ error occurs if we try to import the ‘urllib3‘ module without installing the package or if you have not installed it in the correct environment.

In this tutorial, let’s look at installing the urllib3 module correctly in different operating systems and solve ModuleNotFoundError: No module named ‘urllib3’ error.  

What is ModuleNotFoundError: No module named ‘urllib3’?

There are various reasons why we get the ModuleNotFoundError: No module named ‘urllib3’ error

If you are getting an error installing pip, checkout pip: command not found to resolve the issue.

How to fix ModuleNotFoundError: No module named ‘urllib3’?

urllib3 is not a built-in module (it doesn’t come with the default python installation) in Python; you need to install it explicitly using the pip installer and then use it.  

urllib3 is a powerful, user-friendly HTTP client for Python. Much of the Python ecosystem already uses urllib3 and you should too. urllib3 brings many critical features that are missing from the Python standard libraries.

We can fix the error by installing the ‘urllib3‘ module by running the pip install urllib3 command in your terminal/shell.

We can verify if the package is installed correctly by running the following command in the terminal/shell.

This will provide the details of the package installed, including the version number, license, and the path it is installed. If the module is not installed, you will get a warning message in the terminal stating WARNING: Package(s) not found: urllib3.

pip show urllib3

Output

Name: urllib3
Version: 1.26.11
Summary: HTTP library with thread-safe connection pooling, file post, and more.
Home-page: https://urllib3.readthedocs.io/
Author: Andrey Petrov
Author-email: andrey.petrov@shazow.net
License: MIT
Location: c:\personal\ijs\python_samples\venv\lib\site-packages
Requires:
Required-by: selenium, requests, botocore

Solution 1 – Installing and using the urllib3 module in a proper way

Based on the Python version and the operating system you are running, run the relevant command to install the urllib3 module.

# If you are using Python 2 (Windows)
pip install urllib3

# if you are using Python 3 (Windows)
pip3 install urllib3

# If the pip is not set as environment varibale PATH
python -m pip install urllib3

# If you are using Python 2 (Linux)
sudo pip install urllib3

# if you are using Python 3 (Linux)
sudo pip3 install urllib3

# In case if you have to easy_install
sudo easy_install -U urllib3

# On Centos
yum install urllib3

# On Ubuntu
sudo apt-get install urllib3

# If you are installing it in Anaconda 
conda install -c conda-forge urllib3

Once you have installed the urllib3 module, we can now import it inside our code and use it as shown below.

import urllib3

http = urllib3.PoolManager()
r = http.request("GET", "http://itsmycode.com/robots.txt")
r.status
print(r.data)

Output

b'User-agent: *\r\nDisallow: /wp-admin/\r\nAllow: /wp-admin/admin-ajax.php\r\n\r\nSitemap: https://itsmycode.com/sitemap_index.xml'

Solution 2 – Verify if the IDE is set to use the correct Python version

If you are still getting the same error even after installing the package, you can verify if the IDE you are using is configured with the correct version of the Python interpreter.

For Eg:- In the case of Visual Studio Code, we can set the Python version by pressing CTRL + Shift + Por ( + Shift + P on Mac) to open the command palette.

Once the command palette opens, select the Python interpreter and select the correct version of Python and also the virtual environment(if configured) as shown below.

image 1

Python Interpreter

Solution 3 – Installing urllib3 inside the virtual environment

Many different IDEs like Jupyter Notebook, Spyder, Anaconda, or PyCharm often install their own virtual environment of Python to keep things clean and separated from your global Python.

If you are using VS Code, then you can also create a virtual environment, as shown below.

In the case of virtual environments, you need to ensure that the urllib3 module needs to be installed inside the virtual environment and not globally.

Step 1: Create a Virtual Environment. If you have already created a virtual environment, then proceed to step 2.

Step 2: Activate the Virtual Environment

Step 3: Install the required module using the pip install command

# Create a virtual Environment
py -3 -m venv venv

# Activate the virtual environment (windows command)
venv\Scripts\activate.bat

# Activate the virtual environment (windows powershell)
venv\Scripts\Activate.ps1

# Activate the virtual environment (Linux)
source venv/bin/activate

# Install urllib3 inside the virtual environment
pip install urllib3

Solution 4 – Ensure that a module name is not declared name a variable name.

Last but not least, you may need to cross-check and ensure that you haven’t declared a variable with the same name as the module name.

You should check if you haven’t named any files as urllib3.py as it may shadow the original urllib3 module.

If the issue is still not solved, you can try removing the package and installing it once again, restart the IDE, and check the paths to ensure that packages are installed in the correct environment path and Python version.

Conclusion

The ModuleNotFoundError: No module named ‘urllib3’ error occurs when we try to import the ‘urllib3‘ module without installing the package or if you have not installed it in the correct environment.

We can resolve the issue by installing the urllib3 module by running the pip install urllib3 command. Also, ensure that the module is installed in the proper environment in case you use any virtual environments, and the Python version is appropriately set in the IDE that you are running the code.

August 07, 2022 11:07 AM UTC

ModuleNotFoundError: No module named ‘tqdm’

In Python, ModuleNotFoundError: No module named ‘tqdm’ error occurs if we try to import the ‘tqdm‘ module without installing the package or if you have not installed it in the correct environment.

In this tutorial, let’s look at installing the tqdm module correctly in different operating systems and solve ModuleNotFoundError: No module named ‘tqdm’ error.  

What is ModuleNotFoundError: No module named ‘tqdm’?

There are various reasons why we get the ModuleNotFoundError: No module named ‘tqdm’ error

If you are getting an error installing pip, checkout pip: command not found to resolve the issue.

How to fix ModuleNotFoundError: No module named ‘tqdm’?

tqdm is not a built-in module (it doesn’t come with the default python installation) in Python; you need to install it explicitly using the pip installer and then use it.  

tqdm derives from the Arabic word taqaddum (تقدّم) which can mean “progress,” and is an abbreviation for “I love you so much” in Spanish (te quiero demasiado).

We can fix the error by installing the ‘tqdm‘ module by running the pip install tqdm command in your terminal/shell.

We can verify if the package is installed correctly by running the following command in the terminal/shell.

This will provide the details of the package installed, including the version number, license, and the path it is installed. If the module is not installed, you will get a warning message in the terminal stating WARNING: Package(s) not found: tqdm.

pip show tqdm

Output

Name: tqdm
Version: 8.1.3
Summary: Composable command line interface toolkit
Home-page: https://palletsprojects.com/p/tqdm/   
Author: Armin Ronacher
Author-email: armin.ronacher@active-4.com
License: BSD-3-Clause
Location: c:\personal\ijs\python_samples\venv\lib\site-packages
Requires: colorama
Required-by: black

Solution 1 – Installing and using the tqdm module in a proper way

Based on the Python version and the operating system you are running, run the relevant command to install the tqdm module.

# If you are using Python 2 (Windows)
pip install tqdm

# if you are using Python 3 (Windows)
pip3 install tqdm

# If the pip is not set as environment varibale PATH
python -m pip install tqdm

# If you are using Python 2 (Linux)
sudo pip install tqdm

# if you are using Python 3 (Linux)
sudo pip3 install tqdm

# In case if you have to easy_install
sudo easy_install -U tqdm

# On Centos
yum install tqdm

# On Ubuntu
sudo apt-get install tqdm

# If you are installing it in Anaconda 
conda install -c conda-forge tqdm

Once you have installed the tqdm module, we can now import it inside our code and use it as shown below.

import time
from tqdm import tqdm
for i in tqdm(range(10000)):
    time.sleep(0.001)

Output

image 8ModuleNotFoundError: No module named 'tqdm' 9

Solution 2 – Verify if the IDE is set to use the correct Python version

If you are still getting the same error even after installing the package, you can verify if the IDE you are using is configured with the correct version of the Python interpreter.

For Eg:- In the case of Visual Studio Code, we can set the Python version by pressing CTRL + Shift + Por ( + Shift + P on Mac) to open the command palette.

Once the command palette opens, select the Python interpreter and select the correct version of Python and also the virtual environment(if configured) as shown below.

image 1

Python Interpreter

Solution 3 – Installing tqdm inside the virtual environment

Many different IDEs like Jupyter Notebook, Spyder, Anaconda, or PyCharm often install their own virtual environment of Python to keep things clean and separated from your global Python.

If you are using VS Code, then you can also create a virtual environment, as shown below.

In the case of virtual environments, you need to ensure that the tqdm module needs to be installed inside the virtual environment and not globally.

Step 1: Create a Virtual Environment. If you have already created a virtual environment, then proceed to step 2.

Step 2: Activate the Virtual Environment

Step 3: Install the required module using the pip install command

# Create a virtual Environment
py -3 -m venv venv

# Activate the virtual environment (windows command)
venv\Scripts\activate.bat

# Activate the virtual environment (windows powershell)
venv\Scripts\Activate.ps1

# Activate the virtual environment (Linux)
source venv/bin/activate

# Install tqdm inside the virtual environment
pip install tqdm

Solution 4 – Ensure that a module name is not declared name a variable name.

Last but not least, you may need to cross-check and ensure that you haven’t declared a variable with the same name as the module name.

You should check if you haven’t named any files as tqdm.py as it may shadow the original tqdm module.

If the issue is still not solved, you can try removing the package and installing it once again, restart the IDE, and check the paths to ensure that packages are installed in the correct environment path and Python version.

Conclusion

The ModuleNotFoundError: No module named ‘tqdm’ error occurs when we try to import the ‘tqdm‘ module without installing the package or if you have not installed it in the correct environment.

We can resolve the issue by installing the tqdm module by running the pip install tqdm command. Also, ensure that the module is installed in the proper environment in case you use any virtual environments, and the Python version is appropriately set in the IDE that you are running the code.

August 07, 2022 11:07 AM UTC

ModuleNotFoundError: No module named ‘termcolor’

In Python, ModuleNotFoundError: No module named ‘termcolor’ error occurs if we try to import the ‘termcolor‘ module without installing the package or if you have not installed it in the correct environment.

In this tutorial, let’s look at installing the termcolor module correctly in different operating systems and solve ModuleNotFoundError: No module named ‘termcolor’ error.  

What is ModuleNotFoundError: No module named ‘termcolor’?

There are various reasons why we get the ModuleNotFoundError: No module named ‘termcolor’ error

If you are getting an error installing pip, checkout pip: command not found to resolve the issue.

How to fix ModuleNotFoundError: No module named ‘termcolor’?

termcolor is not a built-in module (it doesn’t come with the default python installation) in Python; you need to install it explicitly using the pip installer and then use it.  

termcolor is a Python package used for ANSII Color formatting for output in the terminal.

We can fix the error by installing the ‘termcolor‘ module by running the pip install termcolor command in your terminal/shell.

We can verify if the package is installed correctly by running the following command in the terminal/shell.

This will provide the details of the package installed, including the version number, license, and the path it is installed. If the module is not installed, you will get a warning message in the terminal stating WARNING: Package(s) not found: termcolor.

pip show termcolor

Output

Version: 1.1.0
Summary: ANSII Color formatting for output in terminal.
Home-page: http://pypi.python.org/pypi/termcolor
Author: Konstantin Lepa
Author-email: konstantin.lepa@gmail.com
License: MIT
Location: c:\personal\ijs\python_samples\venv\lib\site-packages

Solution 1 – Installing and using the termcolor module in a proper way

Based on the Python version and the operating system you are running, run the relevant command to install the termcolor module.

# If you are using Python 2 (Windows)
pip install termcolor

# if you are using Python 3 (Windows)
pip3 install termcolor

# If the pip is not set as environment varibale PATH
python -m pip install termcolor

# If you are using Python 2 (Linux)
sudo pip install termcolor

# if you are using Python 3 (Linux)
sudo pip3 install termcolor

# In case if you have to easy_install
sudo easy_install -U termcolor

# On Centos
yum install termcolor

# On Ubuntu
sudo apt-get install termcolor

# If you are installing it in Anaconda 
conda install -c conda-forge termcolor

Once you have installed the termcolor module, we can now import it inside our code and use it as shown below.

import sys
from termcolor import colored, cprint

text = colored('Hello, World!', 'red', attrs=['reverse', 'blink'])
print(text)

Output

No module named 'termcolor'ModuleNotFoundError: No module named 'termcolor' 12

Solution 2 – Verify if the IDE is set to use the correct Python version

If you are still getting the same error even after installing the package, you can verify if the IDE you are using is configured with the correct version of the Python interpreter.

For Eg:- In the case of Visual Studio Code, we can set the Python version by pressing CTRL + Shift + Por ( + Shift + P on Mac) to open the command palette.

Once the command palette opens, select the Python interpreter and select the correct version of Python and also the virtual environment(if configured) as shown below.

image 1

Python Interpreter

Solution 3 – Installing termcolor inside the virtual environment

Many different IDEs like Jupyter Notebook, Spyder, Anaconda, or PyCharm often install their own virtual environment of Python to keep things clean and separated from your global Python.

If you are using VS Code, then you can also create a virtual environment, as shown below.

In the case of virtual environments, you need to ensure that the termcolor module needs to be installed inside the virtual environment and not globally.

Step 1: Create a Virtual Environment. If you have already created a virtual environment, then proceed to step 2.

Step 2: Activate the Virtual Environment

Step 3: Install the required module using the pip install command

# Create a virtual Environment
py -3 -m venv venv

# Activate the virtual environment (windows command)
venv\Scripts\activate.bat

# Activate the virtual environment (windows powershell)
venv\Scripts\Activate.ps1

# Activate the virtual environment (Linux)
source venv/bin/activate

# Install termcolor inside the virtual environment
pip install termcolor

Solution 4 – Ensure that a module name is not declared name a variable name.

Last but not least, you may need to cross-check and ensure that you haven’t declared a variable with the same name as the module name.

You should check if you haven’t named any files as termcolor.py as it may shadow the original termcolor module.

If the issue is still not solved, you can try removing the package and installing it once again, restart the IDE, and check the paths to ensure that packages are installed in the correct environment path and Python version.

Conclusion

The ModuleNotFoundError: No module named ‘termcolor’ error occurs when we try to import the ‘termcolor‘ module without installing the package or if you have not installed it in the correct environment.

We can resolve the issue by installing the termcolor module by running the pip install termcolor command. Also, ensure that the module is installed in the proper environment in case you use any virtual environments, and the Python version is appropriately set in the IDE that you are running the code.

August 07, 2022 11:07 AM UTC

ModuleNotFoundError: No module named ‘tensorflow’

In Python, ModuleNotFoundError: No module named ‘tensorflow’ error occurs if we try to import the ‘tensorflow‘ module without installing the package or if you have not installed it in the correct environment.

In this tutorial, let’s look at installing the tensorflow module correctly in different operating systems and solve ModuleNotFoundError: No module named ‘tensorflow’ error.  

What is ModuleNotFoundError: No module named ‘tensorflow’?

There are various reasons why we get the ModuleNotFoundError: No module named ‘tensorflow’ error

If you are getting an error installing pip, checkout pip: command not found to resolve the issue.

How to fix ModuleNotFoundError: No module named ‘tensorflow’?

tensorflow is not a built-in module (it doesn’t come with the default python installation) in Python; you need to install it explicitly using the pip installer and then use it.  

TensorFlow makes it easy for beginners and experts to create machine learning models for desktop, mobile, web, and cloud. See the sections below to get started.

We can fix the error by installing the ‘tensorflow‘ module by running the pip install tensorflow command in your terminal/shell.

We can verify if the package is installed correctly by running the following command in the terminal/shell.

This will provide the details of the package installed, including the version number, license, and the path it is installed. If the module is not installed, you will get a warning message in the terminal stating WARNING: Package(s) not found: tensorflow.

pip show tensorflow

Output

Name: tensorflow
Version: 2.9.1
Summary: TensorFlow is an open source machine learning framework for everyone.
Home-page: https://www.tensorflow.org/
Author: Google Inc.
Author-email: packages@tensorflow.org
License: Apache 2.0
Location: c:\personal\ijs\python_samples\venv\lib\site-packages
Requires: h5py, typing-extensions, packaging, six, astunparse, keras-preprocessing, absl-py, flatbuffers, tensorboard, termcolor, gast, setuptools, wrapt, keras, tensorflow-estimator, numpy, opt-einsum, libclang, tensorflow-io-gcs-filesystem, protobuf, google-pasta, grpcio

Solution 1 – Installing and using the tensorflow module in a proper way

Based on the Python version and the operating system you are running, run the relevant command to install the tensorflow module.

# If you are using Python 2 (Windows)
pip install tensorflow

# if you are using Python 3 (Windows)
pip3 install tensorflow

# If the pip is not set as environment varibale PATH
python -m pip install tensorflow

# If you are using Python 2 (Linux)
sudo pip install tensorflow

# if you are using Python 3 (Linux)
sudo pip3 install tensorflow

# In case if you have to easy_install
sudo easy_install -U tensorflow

# On Centos
yum install tensorflow

# On Ubuntu
sudo apt-get install tensorflow

# If you are installing it in Anaconda 
conda install -c conda-forge tensorflow

Once you have installed the tensorflow module, we can now import it inside our code and use it as shown below.

import tensorflow as tf
print("TensorFlow version:", tf.__version__)

Output

TensorFlow version: 2.9.1

Solution 2 – Verify if the IDE is set to use the correct Python version

If you are still getting the same error even after installing the package, you can verify if the IDE you are using is configured with the correct version of the Python interpreter.

For Eg:- In the case of Visual Studio Code, we can set the Python version by pressing CTRL + Shift + Por ( + Shift + P on Mac) to open the command palette.

Once the command palette opens, select the Python interpreter and select the correct version of Python and also the virtual environment(if configured) as shown below.

image 1

Python Interpreter

Solution 3 – Installing tensorflow inside the virtual environment

Many different IDEs like Jupyter Notebook, Spyder, Anaconda, or PyCharm often install their own virtual environment of Python to keep things clean and separated from your global Python.

If you are using VS Code, then you can also create a virtual environment, as shown below.

In the case of virtual environments, you need to ensure that the tensorflow module needs to be installed inside the virtual environment and not globally.

Step 1: Create a Virtual Environment. If you have already created a virtual environment, then proceed to step 2.

Step 2: Activate the Virtual Environment

Step 3: Install the required module using the pip install command

# Create a virtual Environment
py -3 -m venv venv

# Activate the virtual environment (windows command)
venv\Scripts\activate.bat

# Activate the virtual environment (windows powershell)
venv\Scripts\Activate.ps1

# Activate the virtual environment (Linux)
source venv/bin/activate

# Install tensorflow inside the virtual environment
pip install tensorflow

Solution 4 – Ensure that a module name is not declared name a variable name.

Last but not least, you may need to cross-check and ensure that you haven’t declared a variable with the same name as the module name.

You should check if you haven’t named any files as tensorflow.py as it may shadow the original tensorflow module.

If the issue is still not solved, you can try removing the package and installing it once again, restart the IDE, and check the paths to ensure that packages are installed in the correct environment path and Python version.

Conclusion

The ModuleNotFoundError: No module named ‘tensorflow’ error occurs when we try to import the ‘tensorflow‘ module without installing the package or if you have not installed it in the correct environment.

We can resolve the issue by installing the tensorflow module by running the pip install tensorflow command. Also, ensure that the module is installed in the proper environment in case you use any virtual environments, and the Python version is appropriately set in the IDE that you are running the code.

August 07, 2022 11:07 AM UTC

ModuleNotFoundError: No module named ‘tabulate’

In Python, ModuleNotFoundError: No module named ‘tabulate’ error occurs if we try to import the ‘tabulate‘ module without installing the package or if you have not installed it in the correct environment.

In this tutorial, let’s look at installing the tabulate module correctly in different operating systems and solve ModuleNotFoundError: No module named ‘tabulate’ error.  

What is ModuleNotFoundError: No module named ‘tabulate’?

There are various reasons why we get the ModuleNotFoundError: No module named ‘tabulate’ error

If you are getting an error installing pip, checkout pip: command not found to resolve the issue.

How to fix ModuleNotFoundError: No module named ‘tabulate’?

tabulate is not a built-in module (it doesn’t come with the default python installation) in Python; you need to install it explicitly using the pip installer and then use it.  

Pretty-print tabular data in Python is a library and a command-line utility.

The main use cases of the library are:

We can fix the error by installing the ‘tabulate‘ module by running the pip install tabulate command in your terminal/shell.

We can verify if the package is installed correctly by running the following command in the terminal/shell.

This will provide the details of the package installed, including the version number, license, and the path it is installed. If the module is not installed, you will get a warning message in the terminal stating WARNING: Package(s) not found: tabulate.

pip show tabulate

Output

Name: tabulate
Version: 0.8.10
Summary: Pretty-print tabular data
Home-page: https://github.com/astanin/python-tabulate
Author: Sergey Astanin
Author-email: s.astanin@gmail.com
License: MIT
Location: c:\personal\ijs\python_samples\venv\lib\site-packages

Solution 1 – Installing and using the tabulate module in a proper way

Based on the Python version and the operating system you are running, run the relevant command to install the tabulate module.

# If you are using Python 2 (Windows)
pip install tabulate

# if you are using Python 3 (Windows)
pip3 install tabulate

# If the pip is not set as environment varibale PATH
python -m pip install tabulate

# If you are using Python 2 (Linux)
sudo pip install tabulate

# if you are using Python 3 (Linux)
sudo pip3 install tabulate

# In case if you have to easy_install
sudo easy_install -U tabulate

# On Centos
yum install tabulate

# On Ubuntu
sudo apt-get install tabulate

# If you are installing it in Anaconda 
conda install -c conda-forge tabulate

Once you have installed the tabulate module, we can now import it inside our code and use it as shown below.

from tabulate import tabulate

table = [
    ["Sun", 696000, 1989100000],
    ["Earth", 6371, 5973.6],
    ["Moon", 1737, 73.5],
    ["Mars", 3390, 641.85],
]
print(tabulate(table))

Output

-----  ------  -------------
Sun    696000     1.9891e+09
Earth    6371  5973.6       
Moon     1737    73.5       
Mars     3390   641.85      
-----  ------  -------------

Solution 2 – Verify if the IDE is set to use the correct Python version

If you are still getting the same error even after installing the package, you can verify if the IDE you are using is configured with the correct version of the Python interpreter.

For Eg:- In the case of Visual Studio Code, we can set the Python version by pressing CTRL + Shift + Por ( + Shift + P on Mac) to open the command palette.

Once the command palette opens, select the Python interpreter and select the correct version of Python and also the virtual environment(if configured) as shown below.

image 1

Python Interpreter

Solution 3 – Installing tabulate inside the virtual environment

Many different IDEs like Jupyter Notebook, Spyder, Anaconda, or PyCharm often install their own virtual environment of Python to keep things clean and separated from your global Python.

If you are using VS Code, then you can also create a virtual environment, as shown below.

In the case of virtual environments, you need to ensure that the tabulate module needs to be installed inside the virtual environment and not globally.

Step 1: Create a Virtual Environment. If you have already created a virtual environment, then proceed to step 2.

Step 2: Activate the Virtual Environment

Step 3: Install the required module using the pip install command

# Create a virtual Environment
py -3 -m venv venv

# Activate the virtual environment (windows command)
venv\Scripts\activate.bat

# Activate the virtual environment (windows powershell)
venv\Scripts\Activate.ps1

# Activate the virtual environment (Linux)
source venv/bin/activate

# Install tabulate inside the virtual environment
pip install tabulate

Solution 4 – Ensure that a module name is not declared name a variable name.

Last but not least, you may need to cross-check and ensure that you haven’t declared a variable with the same name as the module name.

You should check if you haven’t named any files as tabulate.py as it may shadow the original tabulate module.

If the issue is still not solved, you can try removing the package and installing it once again, restart the IDE, and check the paths to ensure that packages are installed in the correct environment path and Python version.

Conclusion

The ModuleNotFoundError: No module named ‘tabulate’ error occurs when we try to import the ‘tabulate‘ module without installing the package or if you have not installed it in the correct environment.

We can resolve the issue by installing the tabulate module by running the pip install tabulate command. Also, ensure that the module is installed in the proper environment in case you use any virtual environments, and the Python version is appropriately set in the IDE that you are running the code.

August 07, 2022 11:07 AM UTC

ModuleNotFoundError: No module named ‘sqlalchemy’

In Python, ModuleNotFoundError: No module named ‘sqlalchemy’ error occurs if we try to import the ‘SQLAlchemy‘ module without installing the package or if you have not installed it in the correct environment.

In this tutorial, let’s look at installing the SQLAlchemy module correctly in different operating systems and solve ModuleNotFoundError: No module named ‘sqlalchemy’ error.  

What is ModuleNotFoundError: No module named ‘sqlalchemy’?

There are various reasons why we get the ModuleNotFoundError: No module named ‘sqlalchemy’ error

If you are getting an error installing pip, checkout pip: command not found to resolve the issue.

How to fix ModuleNotFoundError: No module named ‘sqlalchemy’?

SQLAlchemy is not a built-in module (it doesn’t come with the default python installation) in Python; you need to install it explicitly using the pip installer and then use it.  

SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL. SQLAlchemy provides a full suite of well known enterprise-level persistence patterns, designed for efficient and high-performing database access, adapted into a simple and Pythonic domain language.

We can fix the error by installing the ‘SQLAlchemy‘ module by running the pip install SQLAlchemy command in your terminal/shell.

We can verify if the package is installed correctly by running the following command in the terminal/shell.

This will provide the details of the package installed, including the version number, license, and the path it is installed. If the module is not installed, you will get a warning message in the terminal stating WARNING: Package(s) not found: SQLAlchemy.

pip show SQLAlchemy

Output

Name: SQLAlchemy
Version: 8.1.3
Summary: Composable command line interface toolkit
Home-page: https://palletsprojects.com/p/SQLAlchemy/   
Author: Armin Ronacher
Author-email: armin.ronacher@active-4.com
License: BSD-3-Clause
Location: c:\personal\ijs\python_samples\venv\lib\site-packages
Requires: colorama
Required-by: black

Solution 1 – Installing and using the SQLAlchemy module in a proper way

Based on the Python version and the operating system you are running, run the relevant command to install the SQLAlchemy module.

# If you are using Python 2 (Windows)
pip install SQLAlchemy

# if you are using Python 3 (Windows)
pip3 install SQLAlchemy

# If the pip is not set as environment varibale PATH
python -m pip install SQLAlchemy

# If you are using Python 2 (Linux)
sudo pip install SQLAlchemy

# if you are using Python 3 (Linux)
sudo pip3 install SQLAlchemy

# In case if you have to easy_install
sudo easy_install -U SQLAlchemy

# On Centos
yum install SQLAlchemy

# On Ubuntu
sudo apt-get install SQLAlchemy

# If you are installing it in Anaconda 
conda install -c conda-forge SQLAlchemy

Once you have installed the SQLAlchemy module, we can now import it inside our code and use it as shown below.

import sqlalchemy

print(sqlalchemy.__version__)

Output

1.4.39

Solution 2 – Verify if the IDE is set to use the correct Python version

If you are still getting the same error even after installing the package, you can verify if the IDE you are using is configured with the correct version of the Python interpreter.

For Eg:- In the case of Visual Studio Code, we can set the Python version by pressing CTRL + Shift + Por ( + Shift + P on Mac) to open the command palette.

Once the command palette opens, select the Python interpreter and select the correct version of Python and also the virtual environment(if configured) as shown below.

image 1

Python Interpreter

Solution 3 – Installing SQLAlchemy inside the virtual environment

Many different IDEs like Jupyter Notebook, Spyder, Anaconda, or PyCharm often install their own virtual environment of Python to keep things clean and separated from your global Python.

If you are using VS Code, then you can also create a virtual environment, as shown below.

In the case of virtual environments, you need to ensure that the SQLAlchemy module needs to be installed inside the virtual environment and not globally.

Step 1: Create a Virtual Environment. If you have already created a virtual environment, then proceed to step 2.

Step 2: Activate the Virtual Environment

Step 3: Install the required module using the pip install command

# Create a virtual Environment
py -3 -m venv venv

# Activate the virtual environment (windows command)
venv\Scripts\activate.bat

# Activate the virtual environment (windows powershell)
venv\Scripts\Activate.ps1

# Activate the virtual environment (Linux)
source venv/bin/activate

# Install SQLAlchemy inside the virtual environment
pip install SQLAlchemy

Solution 4 – Ensure that a module name is not declared name a variable name.

Last but not least, you may need to cross-check and ensure that you haven’t declared a variable with the same name as the module name.

You should check if you haven’t named any files as SQLAlchemy.py as it may shadow the original SQLAlchemy module.

If the issue is still not solved, you can try removing the package and installing it once again, restart the IDE, and check the paths to ensure that packages are installed in the correct environment path and Python version.

Conclusion

The ModuleNotFoundError: No module named ‘sqlalchemy’ error occurs when we try to import the ‘SQLAlchemy‘ module without installing the package or if you have not installed it in the correct environment.

We can resolve the issue by installing the SQLAlchemy module by running the pip install SQLAlchemy command. Also, ensure that the module is installed in the proper environment in case you use any virtual environments, and the Python version is appropriately set in the IDE that you are running the code.

August 07, 2022 11:07 AM UTC

ModuleNotFoundError: No module named ‘sklearn’

In Python, ModuleNotFoundError: No module named ‘sklearn’ error occurs if we try to import the ‘scikit-learn‘ module without installing the package or if you have not installed it in the correct environment.

In this tutorial, let’s look at installing the scikit-learn module correctly in different operating systems and solve ModuleNotFoundError: No module named ‘sklearn’ error.  

What is ModuleNotFoundError: No module named ‘sklearn’?

There are various reasons why we get the ModuleNotFoundError: No module named ‘sklearn’ error

If you are getting an error installing pip, checkout pip: command not found to resolve the issue.

How to fix ModuleNotFoundError: No module named ‘sklearn’?

scikit-learn is not a built-in module (it doesn’t come with the default python installation) in Python; you need to install it explicitly using the pip installer and then use it.  

Scikit-learn is an open-source machine learning library that supports supervised and unsupervised learning. It also provides various tools for model fitting, data preprocessing, model selection, model evaluation, and many other utilities.

We can fix the error by installing the ‘scikit-learn‘ module by running the pip install scikit-learn command in your terminal/shell.

We can verify if the package is installed correctly by running the following command in the terminal/shell.

This will provide the details of the package installed, including the version number, license, and the path it is installed. If the module is not installed, you will get a warning message in the terminal stating WARNING: Package(s) not found: scikit-learn.

pip show scikit-learn

Output

Name: scikit-learn
Version: 8.1.3
Summary: Composable command line interface toolkit
Home-page: https://palletsprojects.com/p/scikit-learn/   
Author: Armin Ronacher
Author-email: armin.ronacher@active-4.com
License: BSD-3-Clause
Location: c:\personal\ijs\python_samples\venv\lib\site-packages
Requires: colorama
Required-by: black

Solution 1 – Installing and using the scikit-learn module in a proper way

Based on the Python version and the operating system you are running, run the relevant command to install the scikit-learn module.

# If you are using Python 2 (Windows)
pip install scikit-learn

# if you are using Python 3 (Windows)
pip3 install scikit-learn

# If the pip is not set as environment varibale PATH
python -m pip install scikit-learn

# If you are using Python 2 (Linux)
sudo pip install scikit-learn

# if you are using Python 3 (Linux)
sudo pip3 install scikit-learn

# In case if you have to easy_install
sudo easy_install -U scikit-learn

# On Centos
yum install scikit-learn

# On Ubuntu
sudo apt-get install scikit-learn

# If you are installing it in Anaconda 
conda install -c anaconda scikit-learn

Once you have installed the scikit-learn module, we can now import it inside our code and use it as shown below.

from sklearn.ensemble import HistGradientBoostingRegressor
import numpy as np
import matplotlib.pyplot as plt

# Simple regression function for X * cos(X)
rng = np.random.RandomState(42)
X_1d = np.linspace(0, 10, num=2000)
X = X_1d.reshape(-1, 1)
y = X_1d * np.cos(X_1d) + rng.normal(scale=X_1d / 3)

quantiles = [0.95, 0.5, 0.05]
parameters = dict(loss="quantile", max_bins=32, max_iter=50)
hist_quantiles = {
    f"quantile={quantile:.2f}": HistGradientBoostingRegressor(
        **parameters, quantile=quantile
    ).fit(X, y)
    for quantile in quantiles
}

fig, ax = plt.subplots()
ax.plot(X_1d, y, "o", alpha=0.5, markersize=1)
for quantile, hist in hist_quantiles.items():
    ax.plot(X_1d, hist.predict(X), label=quantile)
_ = ax.legend(loc="lower left")

Output

No module named 'sklearn'ModuleNotFoundError: No module named 'sklearn' 3

Solution 2 – Verify if the IDE is set to use the correct Python version

If you are still getting the same error even after installing the package, you can verify if the IDE you are using is configured with the correct version of the Python interpreter.

For Eg:- In the case of Visual Studio Code, we can set the Python version by pressing CTRL + Shift + Por ( + Shift + P on Mac) to open the command palette.

Once the command palette opens, select the Python interpreter and select the correct version of Python and also the virtual environment(if configured) as shown below.

image 1

Python Interpreter

Solution 3 – Installing scikit-learn inside the virtual environment

Many different IDEs like Jupyter Notebook, Spyder, Anaconda, or PyCharm often install their own virtual environment of Python to keep things clean and separated from your global Python.

If you are using VS Code, then you can also create a virtual environment, as shown below.

In the case of virtual environments, you need to ensure that the scikit-learn module needs to be installed inside the virtual environment and not globally.

Step 1: Create a Virtual Environment. If you have already created a virtual environment, then proceed to step 2.

Step 2: Activate the Virtual Environment

Step 3: Install the required module using the pip install command

# Create a virtual Environment
py -3 -m venv venv

# Activate the virtual environment (windows command)
venv\Scripts\activate.bat

# Activate the virtual environment (windows powershell)
venv\Scripts\Activate.ps1

# Activate the virtual environment (Linux)
source venv/bin/activate

# Install scikit-learn inside the virtual environment
pip install scikit-learn

Solution 4 – Ensure that a module name is not declared name a variable name.

Last but not least, you may need to cross-check and ensure that you haven’t declared a variable with the same name as the module name.

You should check if you haven’t named any files as sklearn.py it may shadow the original scikit-learn module.

If the issue is still not solved, you can try removing the package and installing it once again, restart the IDE, and check the paths to ensure that packages are installed in the correct environment path and Python version.

Conclusion

The ModuleNotFoundError: No module named ‘sklearn’ error occurs when we try to import the ‘scikit-learn‘ module without installing the package or if you have not installed it in the correct environment.

We can resolve the issue by installing the scikit-learn module by running the pip install scikit-learn command. Also, ensure that the module is installed in the proper environment in case you use any virtual environments, and the Python version is appropriately set in the IDE that you are running the code.

August 07, 2022 09:48 AM UTC

ModuleNotFoundError: No module named ‘skimage’

In Python, ModuleNotFoundError: No module named ‘skimage’ error occurs if we try to import the ‘scikit-image‘ module without installing the package or if you have not installed it in the correct environment.

In this tutorial, let’s look at installing the scikit-image module correctly in different operating systems and solve ModuleNotFoundError: No module named ‘skimage’ error.  

What is ModuleNotFoundError: No module named ‘skimage’?

There are various reasons why we get the ModuleNotFoundError: No module named ‘skimage’ error

If you are getting an error installing pip, checkout pip: command not found to resolve the issue.

How to fix ModuleNotFoundError: No module named ‘skimage’?

scikit-image is not a built-in module (it doesn’t come with the default python installation) in Python; you need to install it explicitly using the pip installer and then use it.  

scikit-image is a collection of algorithms for image processing. It is available free of charge and free of restriction. We pride ourselves on high-quality, peer-reviewed code, written by an active community of volunteers.

We can fix the error by installing the ‘scikit-image‘ module by running the pip install scikit-image command in your terminal/shell.

We can verify if the package is installed correctly by running the following command in the terminal/shell.

This will provide the details of the package installed, including the version number, license, and the path it is installed. If the module is not installed, you will get a warning message in the terminal stating WARNING: Package(s) not found: scikit-image.

pip show scikit-image

Output

Name: scikit-image
Version: 0.19.3
Summary: Image processing in Python
Home-page: https://scikit-image.org
Author:
Author-email:
License: Modified BSD
Location: c:\personal\ijs\python_samples\venv\lib\site-packages
Requires: PyWavelets, scipy, pillow, imageio, packaging, tifffile, numpy, networkx

Solution 1 – Installing and using the scikit-image module in a proper way

Based on the Python version and the operating system you are running, run the relevant command to install the scikit-image module.

# If you are using Python 2 (Windows)
pip install scikit-image

# if you are using Python 3 (Windows)
pip3 install scikit-image

# If the pip is not set as environment varibale PATH
python -m pip install scikit-image

# If you are using Python 2 (Linux)
sudo pip install scikit-image

# if you are using Python 3 (Linux)
sudo pip3 install scikit-image

# In case if you have to easy_install
sudo easy_install -U scikit-image

# On Centos
yum install scikit-image

# On Ubuntu
sudo apt-get install scikit-image

# If you are installing it in Anaconda 
conda install -c anaconda scikit-image

Once you have installed the scikit-image module, we can now import it inside our code and use it as shown below.

from skimage import data, io, filters

image = data.coins()
# ... or any other NumPy array!
edges = filters.sobel(image)
io.imshow(edges)
io.show()

Output

No module named 'skimage'ModuleNotFoundError: No module named 'skimage' 6

Solution 2 – Verify if the IDE is set to use the correct Python version

If you are still getting the same error even after installing the package, you can verify if the IDE you are using is configured with the correct version of the Python interpreter.

For Eg:- In the case of Visual Studio Code, we can set the Python version by pressing CTRL + Shift + Por ( + Shift + P on Mac) to open the command palette.

Once the command palette opens, select the Python interpreter and select the correct version of Python and also the virtual environment(if configured) as shown below.

image 1

Python Interpreter

Solution 3 – Installing scikit-image inside the virtual environment

Many different IDEs like Jupyter Notebook, Spyder, Anaconda, or PyCharm often install their own virtual environment of Python to keep things clean and separated from your global Python.

If you are using VS Code, then you can also create a virtual environment, as shown below.

In the case of virtual environments, you need to ensure that the scikit-image module needs to be installed inside the virtual environment and not globally.

Step 1: Create a Virtual Environment. If you have already created a virtual environment, then proceed to step 2.

Step 2: Activate the Virtual Environment

Step 3: Install the required module using the pip install command

# Create a virtual Environment
py -3 -m venv venv

# Activate the virtual environment (windows command)
venv\Scripts\activate.bat

# Activate the virtual environment (windows powershell)
venv\Scripts\Activate.ps1

# Activate the virtual environment (Linux)
source venv/bin/activate

# Install scikit-image inside the virtual environment
pip install scikit-image

Solution 4 – Ensure that a module name is not declared name a variable name.

Last but not least, you may need to cross-check and ensure that you haven’t declared a variable with the same name as the module name.

You should check if you haven’t named any files as skimage.py as it may shadow the original scikit-image module.

If the issue is still not solved, you can try removing the package and installing it once again, restart the IDE, and check the paths to ensure that packages are installed in the correct environment path and Python version.

Conclusion

The ModuleNotFoundError: No module named ‘skimage’ error occurs when we try to import the ‘scikit-image‘ module without installing the package or if you have not installed it in the correct environment.

We can resolve the issue by installing the scikit-image module by running the pip install scikit-image command. Also, ensure that the module is installed in the proper environment in case you use any virtual environments, and the Python version is appropriately set in the IDE that you are running the code.

August 07, 2022 09:48 AM UTC

ModuleNotFoundError: No module named ‘serial’

In Python, ModuleNotFoundError: No module named ‘serial’ error occurs if we try to import the ‘pyserial‘ module without installing the package or if you have not installed it in the correct environment.

In this tutorial, let’s look at installing the pyserial module correctly in different operating systems and solve ModuleNotFoundError: No module named ‘serial’ error.  

What is ModuleNotFoundError: No module named ‘serial’?

There are various reasons why we get the ModuleNotFoundError: No module named ‘serial’ error

If you are getting an error installing pip, checkout pip: command not found to resolve the issue.

How to fix ModuleNotFoundError: No module named ‘serial’?

pyserial is not a built-in module (it doesn’t come with the default python installation) in Python; you need to install it explicitly using the pip installer and then use it.  

pyserial module encapsulates the access for the serial port. It provides backends for Python running on Windows, OSX, Linux, BSD (possibly any POSIX compliant system) and IronPython. The module named “serial” automatically selects the appropriate backend.l

We can fix the error by installing the ‘pyserial‘ module by running the pip install pyserial command in your terminal/shell.

We can verify if the package is installed correctly by running the following command in the terminal/shell.

This will provide the details of the package installed, including the version number, license, and the path it is installed. If the module is not installed, you will get a warning message in the terminal stating WARNING: Package(s) not found: pyserial.

pip show pyserial

Output

Name: pyserial
Version: 3.5
Summary: Python Serial Port Extension
Home-page: https://github.com/pyserial/pyserial
Author: Chris Liechti
Author-email: cliechti@gmx.net
License: BSD
Location: c:\personal\ijs\python_samples\venv\lib\site-packages

Solution 1 – Installing and using the pyserial module in a proper way

Based on the Python version and the operating system you are running, run the relevant command to install the pyserial module.

# If you are using Python 2 (Windows)
pip install pyserial

# if you are using Python 3 (Windows)
pip3 install pyserial

# If the pip is not set as environment varibale PATH
python -m pip install pyserial

# If you are using Python 2 (Linux)
sudo pip install pyserial

# if you are using Python 3 (Linux)
sudo pip3 install pyserial

# In case if you have to easy_install
sudo easy_install -U pyserial

# On Centos
yum install pyserial

# On Ubuntu
sudo apt-get install pyserial

# If you are installing it in Anaconda 
conda install -c anaconda pyserial

Once you have installed the pyserial module, we can now import it inside our code and use it as shown below.

import serial

print(serial.__version__)

Output

3.5

Solution 2 – Verify if the IDE is set to use the correct Python version

If you are still getting the same error even after installing the package, you can verify if the IDE you are using is configured with the correct version of the Python interpreter.

For Eg:- In the case of Visual Studio Code, we can set the Python version by pressing CTRL + Shift + Por ( + Shift + P on Mac) to open the command palette.

Once the command palette opens, select the Python interpreter and select the correct version of Python and also the virtual environment(if configured) as shown below.

image 1

Python Interpreter

Solution 3 – Installing pyserial inside the virtual environment

Many different IDEs like Jupyter Notebook, Spyder, Anaconda, or PyCharm often install their own virtual environment of Python to keep things clean and separated from your global Python.

If you are using VS Code, then you can also create a virtual environment, as shown below.

In the case of virtual environments, you need to ensure that the pyserial module needs to be installed inside the virtual environment and not globally.

Step 1: Create a Virtual Environment. If you have already created a virtual environment, then proceed to step 2.

Step 2: Activate the Virtual Environment

Step 3: Install the required module using the pip install command

# Create a virtual Environment
py -3 -m venv venv

# Activate the virtual environment (windows command)
venv\Scripts\activate.bat

# Activate the virtual environment (windows powershell)
venv\Scripts\Activate.ps1

# Activate the virtual environment (Linux)
source venv/bin/activate

# Install pyserial inside the virtual environment
pip install pyserial

Solution 4 – Ensure that a module name is not declared name a variable name.

Last but not least, you may need to cross-check and ensure that you haven’t declared a variable with the same name as the module name.

You should check if you haven’t named any files as serial.py as it may shadow the original pyserial module.

If the issue is still not solved, you can try removing the package and installing it once again, restart the IDE, and check the paths to ensure that packages are installed in the correct environment path and Python version.

Conclusion

The ModuleNotFoundError: No module named ‘serial’ error occurs when we try to import the ‘pyserial‘ module without installing the package or if you have not installed it in the correct environment.

We can resolve the issue by installing the pyserial module by running the pip install pyserial command. Also, ensure that the module is installed in the proper environment in case you use any virtual environments, and the Python version is appropriately set in the IDE that you are running the code.

August 07, 2022 09:48 AM UTC

ModuleNotFoundError: No module named ‘selenium’

In Python, ModuleNotFoundError: No module named ‘selenium’ error occurs if we try to import the ‘selenium‘ module without installing the package or if we have not installed it in the correct environment.

In this tutorial, let’s look at installing the selenium module correctly in different operating systems and solve ModuleNotFoundError: No module named ‘selenium’ error.  

What is ModuleNotFoundError: No module named ‘selenium’?

There are various reasons why we get the ModuleNotFoundError: No module named ‘selenium’ error

If you are getting an error installing pip, checkout pip: command not found to resolve the issue.

How to fix ModuleNotFoundError: No module named ‘selenium’?

selenium is not a built-in module (it doesn’t come with the default python installation) in Python; you need to install it explicitly using the pip installer and then use it.  

Selenium automates web browsers. It is most famous for enabling rapid, repeatable web-app testing, which allows developers to ship new releases faster and with confidence.

We can fix the error by installing the ‘selenium‘ module by running the pip install selenium command in your terminal/shell.

We can verify if the package is installed correctly by running the following command in the terminal/shell.

This will provide the details of the package installed, including the version number, license, and the path it is installed. If the module is not installed, you will get a warning message in the terminal stating WARNING: Package(s) not found: selenium.

pip show selenium

Output

Name: selenium
Version: 4.3.0
Summary:
Home-page: https://www.selenium.dev
Author:
Author-email:
License: Apache 2.0
Location: c:\personal\ijs\python_samples\venv\lib\site-packages
Requires: trio, trio-websocket, urllib3

Solution 1 – Installing and using the selenium module in a proper way

Based on the Python version and the operating system you are running, run the relevant command to install the selenium module.

# If you are using Python 2 (Windows)
pip install selenium

# if you are using Python 3 (Windows)
pip3 install selenium

# If the pip is not set as environment varibale PATH
python -m pip install selenium

# If you are using Python 2 (Linux)
sudo pip install selenium

# if you are using Python 3 (Linux)
sudo pip3 install selenium

# In case if you have to easy_install
sudo easy_install -U selenium

# On Centos
yum install selenium

# On Ubuntu
sudo apt-get install selenium

# If you are installing it in Anaconda 
conda install -c conda-forge selenium

Once you have installed the selenium module, we can now import it inside our code and use it as shown below.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox()

browser.get('http://www.yahoo.com')
assert 'Yahoo' in browser.title

elem = browser.find_element(By.NAME, 'p')  # Find the search box
elem.send_keys('seleniumhq' + Keys.RETURN)

browser.quit()

Solution 2 – Verify if the IDE is set to use the correct Python version

If you are still getting the same error even after installing the package, you can verify if the IDE you are using is configured with the correct version of the Python interpreter.

For Eg:- In the case of Visual Studio Code, we can set the Python version by pressing CTRL + Shift + Por ( + Shift + P on Mac) to open the command palette.

Once the command palette opens, select the Python interpreter and select the correct version of Python and also the virtual environment(if configured) as shown below.

image 1

Python Interpreter

Solution 3 – Installing selenium inside the virtual environment

Many different IDEs like Jupyter Notebook, Spyder, Anaconda, or PyCharm often install their own virtual environment of Python to keep things clean and separated from your global Python.

If you are using VS Code, then you can also create a virtual environment, as shown below.

In the case of virtual environments, you need to ensure that the selenium module needs to be installed inside the virtual environment and not globally.

Step 1: Create a Virtual Environment. If you have already created a virtual environment, then proceed to step 2.

Step 2: Activate the Virtual Environment

Step 3: Install the required module using the pip install command

# Create a virtual Environment
py -3 -m venv venv

# Activate the virtual environment (windows command)
venv\Scripts\activate.bat

# Activate the virtual environment (windows powershell)
venv\Scripts\Activate.ps1

# Activate the virtual environment (Linux)
source venv/bin/activate

# Install selenium inside the virtual environment
pip install selenium

Solution 4 – Ensure that a module name is not declared name a variable name.

Last but not least, you may need to cross-check and ensure that you haven’t declared a variable with the same name as the module name.

You should check if you haven’t named any files as selenium.py as it may shadow the original selenium module.

If the issue is still not solved, you can try removing the package and installing it once again, restart the IDE, and check the paths to ensure that packages are installed in the correct environment path and Python version.

Conclusion

The ModuleNotFoundError: No module named ‘selenium’ error occurs when we try to import the ‘selenium‘ module without installing the package or if you have not installed it in the correct environment.

We can resolve the issue by installing the selenium module by running the pip install selenium command. Also, ensure that the module is installed in the proper environment in case you use any virtual environments, and the Python version is appropriately set in the IDE that you are running the code.

August 07, 2022 09:48 AM UTC

ModuleNotFoundError: No module named ‘scipy’

In Python, ModuleNotFoundError: No module named ‘scipy’ error occurs if we try to import the ‘scipy‘ module without installing the package or if you have not installed it in the correct environment.

In this tutorial, let’s look at installing the scipy module correctly in different operating systems and solve ModuleNotFoundError: No module named ‘scipy’ error.  

What is ModuleNotFoundError: No module named ‘scipy’?

There are various reasons why we get the ModuleNotFoundError: No module named ‘scipy’ error

If you are getting an error installing pip, checkout pip: command not found to resolve the issue.

How to fix ModuleNotFoundError: No module named ‘scipy’?

scipy is not a built-in module (it doesn’t come with the default python installation) in Python; you need to install it explicitly using the pip installer and then use it.  

SciPy (pronounced “Sigh Pie”) is open-source software for mathematics, science, and engineering. The SciPy library depends on NumPy, which provides convenient and fast N-dimensional array manipulation. The SciPy library is built to work with NumPy arrays, and provides many user-friendly and efficient numerical routines such as routines for numerical integration and optimization.

We can fix the error by installing the ‘scipy‘ module by running the pip install scipy command in your terminal/shell.

We can verify if the package is installed correctly by running the following command in the terminal/shell.

This will provide the details of the package installed, including the version number, license, and the path it is installed. If the module is not installed, you will get a warning message in the terminal stating WARNING: Package(s) not found: scipy.

pip show scipy

Output

Name: scipy
Version: 1.9.0
Summary: SciPy: Scientific Library for Python
Home-page: https://www.scipy.org
Author:
Author-email:
License: BSD
Location: c:\personal\ijs\python_samples\venv\lib\site-packages
Requires: numpy

Solution 1 – Installing and using the scipy module in a proper way

Based on the Python version and the operating system you are running, run the relevant command to install the scipy module.

# If you are using Python 2 (Windows)
pip install scipy

# if you are using Python 3 (Windows)
pip3 install scipy

# If the pip is not set as environment varibale PATH
python -m pip install scipy

# If you are using Python 2 (Linux)
sudo pip install scipy

# if you are using Python 3 (Linux)
sudo pip3 install scipy

# In case if you have to easy_install
sudo easy_install -U scipy

# On Centos
yum install scipy

# On Ubuntu
sudo apt-get install scipy

# If you are installing it in Anaconda 
conda install -c anaconda scipy

Once you have installed the scipy module, we can now import it inside our code and use it as shown below.

from scipy import constants

print(constants.yotta)
print(constants.zetta)
print(constants.exa)
print(constants.peta)
print(constants.tera)
print(constants.giga)
print(constants.mega)
print(constants.kilo)

Output

1e+24
1e+21
1e+18
1000000000000000.0
1000000000000.0
1000000000.0
1000000.0
1000.0

Solution 2 – Verify if the IDE is set to use the correct Python version

If you are still getting the same error even after installing the package, you can verify if the IDE you are using is configured with the correct version of the Python interpreter.

For Eg:- In the case of Visual Studio Code, we can set the Python version by pressing CTRL + Shift + Por ( + Shift + P on Mac) to open the command palette.

Once the command palette opens, select the Python interpreter and select the correct version of Python and also the virtual environment(if configured) as shown below.

image 1

Python Interpreter

Solution 3 – Installing scipy inside the virtual environment

Many different IDEs like Jupyter Notebook, Spyder, Anaconda, or PyCharm often install their own virtual environment of Python to keep things clean and separated from your global Python.

If you are using VS Code, then you can also create a virtual environment, as shown below.

In the case of virtual environments, you need to ensure that the scipy module needs to be installed inside the virtual environment and not globally.

Step 1: Create a Virtual Environment. If you have already created a virtual environment, then proceed to step 2.

Step 2: Activate the Virtual Environment

Step 3: Install the required module using the pip install command

# Create a virtual Environment
py -3 -m venv venv

# Activate the virtual environment (windows command)
venv\Scripts\activate.bat

# Activate the virtual environment (windows powershell)
venv\Scripts\Activate.ps1

# Activate the virtual environment (Linux)
source venv/bin/activate

# Install scipy inside the virtual environment
pip install scipy

Solution 4 – Ensure that a module name is not declared name a variable name.

Last but not least, you may need to cross-check and ensure that you haven’t declared a variable with the same name as the module name.

You should check if you haven’t named any files as scipy.py as it may shadow the original scipy module.

If the issue is still not solved, you can try removing the package and installing it once again, restart the IDE, and check the paths to ensure that packages are installed in the correct environment path and Python version.

Conclusion

The ModuleNotFoundError: No module named ‘scipy’ error occurs when we try to import the ‘scipy‘ module without installing the package or if you have not installed it in the correct environment.

We can resolve the issue by installing the scipy module by running the pip install scipy command. Also, ensure that the module is installed in the proper environment in case you use any virtual environments, and the Python version is appropriately set in the IDE that you are running the code.

August 07, 2022 09:48 AM UTC

ModuleNotFoundError: No module named ‘requests’

In Python, ModuleNotFoundError: No module named ‘requests’ error occurs if we try to import the ‘requests‘ module without installing the package or if you have not installed it in the correct environment.

In this tutorial, let’s look at installing the requests module correctly in different operating systems and solve ModuleNotFoundError: No module named ‘requests’ error.  

What is ModuleNotFoundError: No module named ‘requests’?

There are various reasons why we get the ModuleNotFoundError: No module named ‘requests’ error

If you are getting an error installing pip, checkout pip: command not found to resolve the issue.

How to fix ModuleNotFoundError: No module named ‘requests’?

requests is not a built-in module (it doesn’t come with the default python installation) in Python; you need to install it explicitly using the pip installer and then use it.  

Requests is an elegant and simple HTTP library for Python, built for human beings.

Requests allow you to send HTTP/1.1 requests extremely easily. There’s no need to manually add query strings to your URLs, or to form-encode your POST data. Keep-alive and HTTP connection pooling are 100% automatic, thanks to urllib3.

We can fix the error by installing the ‘requests‘ module by running the pip install requests command in your terminal/shell.

We can verify if the package is installed correctly by running the following command in the terminal/shell.

This will provide the details of the package installed, including the version number, license, and the path it is installed. If the module is not installed, you will get a warning message in the terminal stating WARNING: Package(s) not found: requests.

pip show requests

Output

Name: requests
Version: 2.28.1
Summary: Python HTTP for Humans.
Home-page: https://requests.readthedocs.io
Author: Kenneth Reitz
Author-email: me@kennethreitz.org
License: Apache 2.0
Location: c:\personal\ijs\python_samples\venv\lib\site-packages
Requires: charset-normalizer, idna, urllib3, certifi
Required-by: Nakedx

Solution 1 – Installing and using the requests module in a proper way

Based on the Python version and the operating system you are running, run the relevant command to install the requests module.

# If you are using Python 2 (Windows)
pip install requests

# if you are using Python 3 (Windows)
pip3 install requests

# If the pip is not set as environment varibale PATH
python -m pip install requests

# If you are using Python 2 (Linux)
sudo pip install requests

# if you are using Python 3 (Linux)
sudo pip3 install requests

# In case if you have to easy_install
sudo easy_install -U requests

# On Centos
yum install python-requests

# On Ubuntu
sudo apt-get install python3-requests

# If you are installing it in Anaconda 
conda install -c anaconda requests

Once you have installed the requests module, we can now import it inside our code and use it as shown below.

import requests


def get_data():
    res = requests.get("https://jsonplaceholder.typicode.com/posts/1")
    print(res.json())


get_data()

Output

{'userId': 1, 'id': 1, 'title': 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit', 'body': 'quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas 
totam\nnostrum rerum est autem sunt rem eveniet architecto'}

Solution 2 – Verify if the IDE is set to use the correct Python version

If you are still getting the same error even after installing the package, you can verify if the IDE you are using is configured with the correct version of the Python interpreter.

For Eg:- In the case of Visual Studio Code, we can set the Python version by pressing CTRL + Shift + Por ( + Shift + P on Mac) to open the command palette.

Once the command palette opens, select the Python interpreter and select the correct version of Python and also the virtual environment(if configured) as shown below.

image 1

Python Interpreter

Solution 3 – Installing requests inside the virtual environment

Many different IDEs like Jupyter Notebook, Spyder, Anaconda, or PyCharm often install their own virtual environment of Python to keep things clean and separated from your global Python.

If you are using VS Code, then you can also create a virtual environment, as shown below.

In the case of virtual environments, you need to ensure that the requests module needs to be installed inside the virtual environment and not globally.

Step 1: Create a Virtual Environment. If you have already created a virtual environment, then proceed to step 2.

Step 2: Activate the Virtual Environment

Step 3: Install the required module using the pip install command

# Create a virtual Environment
py -3 -m venv venv

# Activate the virtual environment (windows command)
venv\Scripts\activate.bat

# Activate the virtual environment (windows powershell)
venv\Scripts\Activate.ps1

# Activate the virtual environment (Linux)
source venv/bin/activate

# Install requests inside the virtual environment
pip install requests

Solution 4 – Ensure that a module name is not declared name a variable name.

Last but not least, you may need to cross-check and ensure that you haven’t declared a variable with the same name as the module name.

You should check if you haven’t named any files as requests.py as it may shadow the original requests module.

If the issue is still not solved, you can try removing the package and installing it once again, restart the IDE, and check the paths to ensure that packages are installed in the correct environment path and Python version.

Conclusion

The ModuleNotFoundError: No module named ‘requests’ error occurs when we try to import the ‘requests‘ module without installing the package or if you have not installed it in the correct environment.

We can resolve the issue by installing the requests module by running the pip install requests command. Also, ensure that the module is installed in the proper environment in case you use any virtual environments, and the Python version is appropriately set in the IDE that you are running the code.

August 07, 2022 09:14 AM UTC

ModuleNotFoundError: No module named ‘redis’

In Python, ModuleNotFoundError: No module named ‘redis’ error occurs if we try to import the ‘redis‘ module without installing the package or if you have not installed it in the correct environment.

In this tutorial, let’s look at installing the redis module correctly in different operating systems and solve ModuleNotFoundError: No module named ‘redis’ error.  

What is ModuleNotFoundError: No module named ‘redis’?

There are various reasons why we get the ModuleNotFoundError: No module named ‘redis’ error

If you are getting an error installing pip, checkout pip: command not found to resolve the issue.

How to fix ModuleNotFoundError: No module named ‘redis’?

redis is not a built-in module (it doesn’t come with the default python installation) in Python; you need to install it explicitly using the pip installer and then use it.  


Redis is an in-memory key-value pair database typically classified as a NoSQL database. Redis is commonly used for caching, transient data storage and as a holding area for data during analysis in Python applications.

We can fix the error by installing the ‘redis‘ module by running the pip install redis command in your terminal/shell.

We can verify if the package is installed correctly by running the following command in the terminal/shell.

This will provide the details of the package installed, including the version number, license, and the path it is installed. If the module is not installed, you will get a warning message in the terminal stating WARNING: Package(s) not found: redis.

pip show redis

Output

Name: redis
Version: 4.3.4
Summary: Python client for Redis database and key-value store
Home-page: https://github.com/redis/redis-py
Author: Redis Inc.
Author-email: oss@redis.com
License: MIT
Location: c:\personal\ijs\python_samples\venv\lib\site-packages
Requires: packaging, async-timeout, deprecated

Solution 1 – Installing and using the redis module in a proper way

Based on the Python version and the operating system you are running, run the relevant command to install the redis module.

# If you are using Python 2 (Windows)
pip install redis

# if you are using Python 3 (Windows)
pip3 install redis

# If the pip is not set as environment varibale PATH
python -m pip install redis

# If you are using Python 2 (Linux)
sudo pip install redis

# if you are using Python 3 (Linux)
sudo pip3 install redis

# In case if you have to easy_install
sudo easy_install -U redis

# On Centos
yum install redis

# On Ubuntu
sudo apt-get install redis

# If you are installing it in Anaconda 
conda install -c conda-forge redis-py

Once you have installed the redis module, we can now import it inside our code and use it as shown below.

import redis
r = redis.Redis(host='localhost', port=6379, db=0)
r.set('foo', 'bar')
r.get('foo')

Output

b'bar'

Solution 2 – Verify if the IDE is set to use the correct Python version

If you are still getting the same error even after installing the package, you can verify if the IDE you are using is configured with the correct version of the Python interpreter.

For Eg:- In the case of Visual Studio Code, we can set the Python version by pressing CTRL + Shift + Por ( + Shift + P on Mac) to open the command palette.

Once the command palette opens, select the Python interpreter and select the correct version of Python and also the virtual environment(if configured) as shown below.

image 1

Python Interpreter

Solution 3 – Installing redis inside the virtual environment

Many different IDEs like Jupyter Notebook, Spyder, Anaconda, or PyCharm often install their own virtual environment of Python to keep things clean and separated from your global Python.

If you are using VS Code, then you can also create a virtual environment, as shown below.

In the case of virtual environments, you need to ensure that the redis module needs to be installed inside the virtual environment and not globally.

Step 1: Create a Virtual Environment. If you have already created a virtual environment, then proceed to step 2.

Step 2: Activate the Virtual Environment

Step 3: Install the required module using the pip install command

# Create a virtual Environment
py -3 -m venv venv

# Activate the virtual environment (windows command)
venv\Scripts\activate.bat

# Activate the virtual environment (windows powershell)
venv\Scripts\Activate.ps1

# Activate the virtual environment (Linux)
source venv/bin/activate

# Install redis inside the virtual environment
pip install redis

Solution 4 – Ensure that a module name is not declared name a variable name.

Last but not least, you may need to cross-check and ensure that you haven’t declared a variable with the same name as the module name.

You should check if you haven’t named any files as redis.py as it may shadow the original redis module.

If the issue is still not solved, you can try removing the package and installing it once again, restart the IDE, and check the paths to ensure that packages are installed in the correct environment path and Python version.

Conclusion

The ModuleNotFoundError: No module named ‘redis’ error occurs when we try to import the ‘redis‘ module without installing the package or if you have not installed it in the correct environment.

We can resolve the issue by installing the redis module by running the pip install redis command. Also, ensure that the module is installed in the proper environment in case you use any virtual environments, and the Python version is appropriately set in the IDE that you are running the code.

August 07, 2022 09:13 AM UTC

ModuleNotFoundError: No module named ‘Queue’

In Python, ModuleNotFoundError: No module named ‘Queue’ error occurs if we try to import the ‘queue‘ module incorrectly or if we have our module named queue.py which shadows the original module.

In this tutorial, let’s look at installing the queue module correctly in different operating systems and solve ModuleNotFoundError: No module named ‘Queue’ error.  

What is ModuleNotFoundError: No module named ‘Queue’?

There are various reasons why we get the ModuleNotFoundError: No module named ‘Queue’ error

How to fix ModuleNotFoundError: No module named ‘Queue’?

The queue is built-in module of Python which is used to implement a queue. We can fix the error by following the solutions provided below.

Solution 1 – Installing and using the queue module in a proper way

In Python 3, the import of queue is with lowercase ‘q’ and in Python 2 it would be with uppercase ‘Q’

from queue import Queue

# Initializing a queue
q = Queue(maxsize=3)

# qsize() give the maxsize
# of the Queue
print("Queue Size is",q.qsize())

# Adding of element to queue
q.put("a")
q.put("b")
q.put("c")

# Return Boolean for Full
# Queue
print("Is Full: ", q.full())


# Removing element from queue
print("Elements dequeued from the queue")
print(q.get())
print(q.get())
print(q.get())

print("Is Full: ", q.full())
print("Queue Size is",q.qsize())

Output

Queue Size is 0
Is Full:  True
Elements dequeued from the queue
a
b
c
Is Full:  False
Queue Size is 0

Solution 2 – Verify if the IDE is set to use the correct Python version

If you are still getting the same error even after installing the package, you can verify if the IDE you are using is configured with the correct version of the Python interpreter.

For Eg:- In the case of Visual Studio Code, we can set the Python version by pressing CTRL + Shift + Por ( + Shift + P on Mac) to open the command palette.

Once the command palette opens, select the Python interpreter and select the correct version of Python and also the virtual environment(if configured) as shown below.

image 1

Python Interpreter

Solution 3 – Installing queue inside the virtual environment

Many different IDEs like Jupyter Notebook, Spyder, Anaconda, or PyCharm often install their own virtual environment of Python to keep things clean and separated from your global Python.

If you are using VS Code, then you can also create a virtual environment, as shown below.

In the case of virtual environments, you need to ensure that the queue module needs to be installed inside the virtual environment and not globally.

Step 1: Create a Virtual Environment. If you have already created a virtual environment, then proceed to step 2.

Step 2: Activate the Virtual Environment

Step 3: Install the required module using the pip install command

# Create a virtual Environment
py -3 -m venv venv

# Activate the virtual environment (windows command)
venv\Scripts\activate.bat

# Activate the virtual environment (windows powershell)
venv\Scripts\Activate.ps1

# Activate the virtual environment (Linux)
source venv/bin/activate

# Install queue inside the virtual environment
pip install queue

Solution 4 – Ensure that a module name is not declared name a variable name.

Last but not least, you may need to cross-check and ensure that you haven’t declared a variable with the same name as the module name.

You should check if you haven’t named any files as queue.py as it may shadow the original queue module.

If the issue is still not solved, you can try removing the package and installing it once again, restart the IDE, and check the paths to ensure that packages are installed in the correct environment path and Python version.

Conclusion

In Python, ModuleNotFoundError: No module named ‘Queue’ error occurs if we try to import the ‘queue‘ module incorrectly or if we have our module named queue.py which shadows the original module.

We can resolve the issue by installing the queue module by importing the queue properly (from queue import Queue). Also, ensure that the module is installed in the proper environment in case you use any virtual environments, and the Python version is appropriately set in the IDE that you are running the code.

August 07, 2022 09:13 AM UTC

ModuleNotFoundError: No module named ‘pytest’

In Python, ModuleNotFoundError: No module named ‘pytest’ error occurs if we try to import the ‘pytest‘ module without installing the package or if you have not installed it in the correct environment.

In this tutorial, let’s look at installing the pytest module correctly in different operating systems and solve ModuleNotFoundError: No module named ‘pytest’ error.  

What is ModuleNotFoundError: No module named ‘pytest’?

There are various reasons why we get the ModuleNotFoundError: No module named ‘pytest’ error

If you are getting an error installing pip, checkout pip: command not found to resolve the issue.

How to fix ModuleNotFoundError: No module named ‘pytest’?

pytest is not a built-in module (it doesn’t come with the default python installation) in Python; you need to install it explicitly using the pip installer and then use it.  

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries.

We can fix the error by installing the ‘pytest‘ module by running the pip install pytest command in your terminal/shell.

We can verify if the package is installed correctly by running the following command in the terminal/shell.

This will provide the details of the package installed, including the version number, license, and the path it is installed. If the module is not installed, you will get a warning message in the terminal stating WARNING: Package(s) not found: pytest.

pip show pytest

Output

Name: pytest
Version: 7.1.2
Summary: pytest: simple powerful testing with Python
Home-page: https://docs.pytest.org/en/latest/
Author: Holger Krekel, Bruno Oliveira, Ronny Pfannschmidt, Floris Bruynooghe, Brianna Laugher, Florian Bruhin and others
Author-email:
License: MIT
Location: c:\personal\ijs\python_samples\venv\lib\site-packages
Requires: colorama, packaging, py, atomicwrites, tomli, pluggy, iniconfig, attrs

Solution 1 – Installing and using the pytest module in a proper way

Based on the Python version and the operating system you are running, run the relevant command to install the pytest module.

# If you are using Python 2 (Windows)
pip install pytest

# if you are using Python 3 (Windows)
pip3 install pytest

# If the pip is not set as environment varibale PATH
python -m pip install pytest

# If you are using Python 2 (Linux)
sudo pip install pytest

# if you are using Python 3 (Linux)
sudo pip3 install pytest

# In case if you have to easy_install
sudo easy_install -U pytest

# On Centos
yum install pytest

# On Ubuntu
sudo apt-get install pytest

# If you are installing it in Anaconda 
conda install -c anaconda pytest

Once you have installed the pytest module, we can now import it inside our code and use it as shown below.

# content of test_sample.py
def inc(x):
    return x + 1


def test_answer():
    assert inc(3) == 5

Output

$ pytest
============================= test session starts =============================
collected 1 items

test_sample.py F

================================== FAILURES ===================================
_________________________________ test_answer _________________________________

    def test_answer():
>       assert inc(3) == 5
E       assert 4 == 5
E        +  where 4 = inc(3)

test_sample.py:5: AssertionError
========================== 1 failed in 0.04 seconds ===========================

Solution 2 – Verify if the IDE is set to use the correct Python version

If you are still getting the same error even after installing the package, you can verify if the IDE you are using is configured with the correct version of the Python interpreter.

For Eg:- In the case of Visual Studio Code, we can set the Python version by pressing CTRL + Shift + Por ( + Shift + P on Mac) to open the command palette.

Once the command palette opens, select the Python interpreter and select the correct version of Python and also the virtual environment(if configured) as shown below.

image 1

Python Interpreter

Solution 3 – Installing pytest inside the virtual environment

Many different IDEs like Jupyter Notebook, Spyder, Anaconda, or PyCharm often install their own virtual environment of Python to keep things clean and separated from your global Python.

If you are using VS Code, then you can also create a virtual environment, as shown below.

In the case of virtual environments, you need to ensure that the pytest module needs to be installed inside the virtual environment and not globally.

Step 1: Create a Virtual Environment. If you have already created a virtual environment, then proceed to step 2.

Step 2: Activate the Virtual Environment

Step 3: Install the required module using the pip install command

# Create a virtual Environment
py -3 -m venv venv

# Activate the virtual environment (windows command)
venv\Scripts\activate.bat

# Activate the virtual environment (windows powershell)
venv\Scripts\Activate.ps1

# Activate the virtual environment (Linux)
source venv/bin/activate

# Install pytest inside the virtual environment
pip install pytest

Solution 4 – Ensure that a module name is not declared name a variable name.

Last but not least, you may need to cross-check and ensure that you haven’t declared a variable with the same name as the module name.

You should check if you haven’t named any files as pytest.py as it may shadow the original pytest module.

If the issue is still not solved, you can try removing the package and installing it once again, restart the IDE, and check the paths to ensure that packages are installed in the correct environment path and Python version.

Conclusion

The ModuleNotFoundError: No module named ‘pytest’ error occurs when we try to import the ‘pytest‘ module without installing the package or if you have not installed it in the correct environment.

We can resolve the issue by installing the pytest module by running the pip install pytest command. Also, ensure that the module is installed in the proper environment in case you use any virtual environments, and the Python version is appropriately set in the IDE that you are running the code.

August 07, 2022 09:13 AM UTC


PyCharm

The PyCharm 2022.2.1 Release Candidate Is Now Available!

We’re working closely with your feedback on the PyCharm 2022.2 version that was released last week. Thank you for sharing with us! If you encounter an issue in PyCharm 2022.2, please reach out to our support team. We are actively fixing regressions, and will be delivering a minor bug-fix release soon.

You can get the new build from our page, via the free Toolbox App, or by using snaps for Ubuntu. 

Today, the release candidate for the first minor update is available. Here are the major fixes:

We’re working on fixes for the following recent regressions with local and remote interpreters – stay tuned:

If you encounter any bugs or have feedback to share, please submit it to our issue tracker, via Twitter, or in the comments section of this blog post.

August 07, 2022 08:22 AM UTC

August 06, 2022


Test and Code

192: Learn to code through game development with PursuedPyBear - Piper Thunstrom

The first game I remember coding, or at least copying from a magazine, was in Basic. It was Lunar Lander.

Learning to code a game is a way that a lot of people get started and excited about programming.

Of course, I don't recommend Basic. Now we've got Python. And one of the game engines available for Python is PursuedPyBear, a project started by Piper Thunstrom.

Piper joins us this episode and we talk about PursuedPyBear, learning to code, and learning CS concepts with game development.

PursuedPyBear, ppb, is a game framework great for learning with, with goals of being fun, education friendly, an example of idiomatic Python, hardware library agnostic, and built on event driven and object oriented concepts.

Special Guest: Piper Thunstrom.

Sponsored By:

Links:

<p>The first game I remember coding, or at least copying from a magazine, was in Basic. It was Lunar Lander. </p> <p>Learning to code a game is a way that a lot of people get started and excited about programming. </p> <p>Of course, I don&#39;t recommend Basic. Now we&#39;ve got Python. And one of the game engines available for Python is PursuedPyBear, a project started by Piper Thunstrom. </p> <p>Piper joins us this episode and we talk about PursuedPyBear, learning to code, and learning CS concepts with game development. </p> <p>PursuedPyBear, ppb, is a game framework great for learning with, with goals of being fun, education friendly, an example of idiomatic Python, hardware library agnostic, and built on event driven and object oriented concepts.</p><p>Special Guest: Piper Thunstrom.</p><p>Sponsored By:</p><ul><li><a href="http://rollbar.com/testandcode" rel="nofollow">Rollbar</a>: <a href="http://rollbar.com/testandcode" rel="nofollow">With Rollbar, developers deploy better software faster.</a></li></ul><p>Links:</p><ul><li><a href="https://ppb.dev/" title="PursuedPyBear | Unbearably Fun Game Development" rel="nofollow">PursuedPyBear | Unbearably Fun Game Development</a></li><li><a href="https://piper.thunstrom.dev/" title="Piper's Blog" rel="nofollow">Piper's Blog</a></li><li><a href="https://www.youtube.com/watch?v=bbKED0o3uVU" title="Making Games With PPB - PyTexas" rel="nofollow">Making Games With PPB - PyTexas</a></li><li><a href="https://pathunstrom.itch.io/shooter-game" title="Shooter Game by Piper Thunstrom" rel="nofollow">Shooter Game by Piper Thunstrom</a></li><li><a href="https://github.com/pathunstrom/shootergame" title="shootergame on GitHub" rel="nofollow">shootergame on GitHub</a></li><li><a href="https://beeware.org/project/projects/tools/briefcase/" title="Briefcase— BeeWare" rel="nofollow">Briefcase— BeeWare</a></li><li><a href="https://github.com/pathunstrom/game-blink" title="game-blink: A tiny emergent behavior toy." rel="nofollow">game-blink: A tiny emergent behavior toy.</a></li><li><a href="https://en.wikipedia.org/wiki/Combat_(Atari_2600)" title="Combat (Atari 2600)" rel="nofollow">Combat (Atari 2600)</a> &mdash; The tank game I didn't remember the name of.</li><li><a href="https://en.wikipedia.org/wiki/Lunar_Lander_(video_game_genre)" title="Lunar Lander" rel="nofollow">Lunar Lander</a></li></ul>

August 06, 2022 11:15 PM UTC


ItsMyCode

ModuleNotFoundError: No module named ‘psycopg2’

In Python, ModuleNotFoundError: No module named ‘psycopg2’ error occurs if we try to import the ‘psycopg2-binary‘ module without installing the package or if you have not installed it in the correct environment.

In this tutorial, let’s look at installing the psycopg2-binary module correctly in different operating systems and solve ModuleNotFoundError: No module named ‘psycopg2’ error.  

What is ModuleNotFoundError: No module named ‘psycopg2’?

There are various reasons why we get the ModuleNotFoundError: No module named ‘psycopg2’ error

If you are getting an error installing pip, checkout pip: command not found to resolve the issue.

How to fix ModuleNotFoundError: No module named ‘psycopg2’?

psycopg2-binary is not a built-in module (it doesn’t come with the default python installation) in Python; you need to install it explicitly using the pip installer and then use it.  

Psycopg is the most popular PostgreSQL database adapter for the Python programming language. Its main features are the complete implementation of the Python DB API 2.0 specification and the thread safety (several threads can share the same connection).

We can fix the error by installing the ‘psycopg2-binary‘ module by running the pip install psycopg2-binary command in your terminal/shell.

We can verify if the package is installed correctly by running the following command in the terminal/shell.

This will provide the details of the package installed, including the version number, license, and the path it is installed. If the module is not installed, you will get a warning message in the terminal stating WARNING: Package(s) not found: psycopg2-binary.

pip show psycopg2-binary

Output

Name: psycopg2
Version: 2.9.3
Summary: psycopg2 - Python-PostgreSQL Database Adapter
Home-page: https://psycopg.org/
Author: Federico Di Gregorio
Author-email: fog@initd.org
License: LGPL with exceptions
Location: c:\personal\ijs\python_samples\venv\lib\site-packages

Solution 1 – Installing and using the psycopg2-binary module in a proper way

Based on the Python version and the operating system you are running, run the relevant command to install the psycopg2-binary module.

# If you are using Python 2 (Windows)
pip install psycopg2-binary

# if you are using Python 3 (Windows)
pip3 install psycopg2-binary

# If the pip is not set as environment varibale PATH
python -m pip install psycopg2-binary

# If you are using Python 2 (Linux)
sudo pip install psycopg2-binary

# if you are using Python 3 (Linux)
sudo pip3 install psycopg2-binary

# In case if you have to easy_install
sudo easy_install -U psycopg2-binary

# On Centos
yum install psycopg2-binary

# On Ubuntu
sudo apt-get install psycopg2-binary

# If you are installing it in Anaconda 
conda install -c conda-forge psycopg2-binary

Once you have installed the psycopg2-binary module, we can now import it inside our code and use it as shown below.

import psycopg2

# Connect to your postgres DB
conn = psycopg2.connect("dbname=test user=postgres")

# Open a cursor to perform database operations
cur = conn.cursor()

# Execute a query
cur.execute("SELECT * FROM my_data")

# Retrieve query results

Solution 2 – Verify if the IDE is set to use the correct Python version

If you are still getting the same error even after installing the package, you can verify if the IDE you are using is configured with the correct version of the Python interpreter.

For Eg:- In the case of Visual Studio Code, we can set the Python version by pressing CTRL + Shift + Por ( + Shift + P on Mac) to open the command palette.

Once the command palette opens, select the Python interpreter and select the correct version of Python and also the virtual environment(if configured) as shown below.

image 1

Python Interpreter

Solution 3 – Installing psycopg2-binary inside the virtual environment

Many different IDEs like Jupyter Notebook, Spyder, Anaconda, or PyCharm often install their own virtual environment of Python to keep things clean and separated from your global Python.

If you are using VS Code, then you can also create a virtual environment, as shown below.

In the case of virtual environments, you need to ensure that the psycopg2-binary module needs to be installed inside the virtual environment and not globally.

Step 1: Create a Virtual Environment. If you have already created a virtual environment, then proceed to step 2.

Step 2: Activate the Virtual Environment

Step 3: Install the required module using the pip install command

# Create a virtual Environment
py -3 -m venv venv

# Activate the virtual environment (windows command)
venv\Scripts\activate.bat

# Activate the virtual environment (windows powershell)
venv\Scripts\Activate.ps1

# Activate the virtual environment (Linux)
source venv/bin/activate

# Install psycopg2-binary inside the virtual environment
pip install psycopg2-binary

Solution 4 – Ensure that a module name is not declared name a variable name.

Last but not least, you may need to cross-check and ensure that you haven’t declared a variable with the same name as the module name.

You should check if you haven’t named any files as psycopg2.py as it may shadow the original psycopg2-binary module.

If the issue is still not solved, you can try removing the package and installing it once again, restart the IDE, and check the paths to ensure that packages are installed in the correct environment path and Python version.

Conclusion

The ModuleNotFoundError: No module named ‘psycopg2’ error occurs when we try to import the ‘psycopg2-binary‘ module without installing the package or if you have not installed it in the correct environment.

We can resolve the issue by installing the psycopg2-binary module by running the pip install psycopg2-binary command. Also, ensure that the module is installed in the proper environment in case you use any virtual environments, and the Python version is appropriately set in the IDE that you are running the code.

August 06, 2022 07:51 PM UTC

ModuleNotFoundError: No module named ‘psutil’

In Python, ModuleNotFoundError: No module named ‘psutil’ error occurs if we try to import the ‘psutil‘ module without installing the package or if you have not installed it in the correct environment.

In this tutorial, let’s look at installing the psutil module correctly in different operating systems and solve ModuleNotFoundError: No module named ‘psutil’ error.  

What is ModuleNotFoundError: No module named ‘psutil’?

There are various reasons why we get the ModuleNotFoundError: No module named ‘psutil’ error

If you are getting an error installing pip, checkout pip: command not found to resolve the issue.

How to fix ModuleNotFoundError: No module named ‘psutil’?

psutil is not a built-in module (it doesn’t come with the default python installation) in Python; you need to install it explicitly using the pip installer and then use it.  

psutil (process and system utilities) is a cross-platform library for retrieving information on running processes and system utilization (CPU, memory, disks, network, sensors) in Python. It is useful mainly for system monitoringprofiling and limiting process resources and management of running processes

We can fix the error by installing the ‘psutil‘ module by running the pip install psutil command in your terminal/shell.

We can verify if the package is installed correctly by running the following command in the terminal/shell.

This will provide the details of the package installed, including the version number, license, and the path it is installed. If the module is not installed, you will get a warning message in the terminal stating WARNING: Package(s) not found: psutil.

pip show psutil

Output

Name: psutil
Version: 5.9.1
Summary: Cross-platform lib for process and system monitoring in Python.
Home-page: https://github.com/giampaolo/psutil
Author: Giampaolo Rodola
Author-email: g.rodola@gmail.com
License: BSD
Location: c:\personal\ijs\python_samples\venv\lib\site-packages

Solution 1 – Installing and using the psutil module in a proper way

Based on the Python version and the operating system you are running, run the relevant command to install the psutil module.

# If you are using Python 2 (Windows)
pip install psutil

# if you are using Python 3 (Windows)
pip3 install psutil

# If the pip is not set as environment varibale PATH
python -m pip install psutil

# If you are using Python 2 (Linux)
sudo pip install psutil

# if you are using Python 3 (Linux)
sudo pip3 install psutil

# In case if you have to easy_install
sudo easy_install -U psutil

# On Centos
yum install psutil

# On Ubuntu
sudo apt-get install psutil

# If you are installing it in Anaconda 
conda install -c conda-forge psutil

Once you have installed the psutil module, we can now import it inside our code and use it as shown below.

import psutil

print(psutil.cpu_times())
print(psutil.virtual_memory())

Output

scputimes(user=679134.796875, system=319145.4375, idle=3937574.28125, interrupt=19485.859375, dpc=23378.8125)
svmem(total=16942211072, available=2417156096, percent=85.7, used=14525054976, free=2417156096)

Solution 2 – Verify if the IDE is set to use the correct Python version

If you are still getting the same error even after installing the package, you can verify if the IDE you are using is configured with the correct version of the Python interpreter.

For Eg:- In the case of Visual Studio Code, we can set the Python version by pressing CTRL + Shift + Por ( + Shift + P on Mac) to open the command palette.

Once the command palette opens, select the Python interpreter and select the correct version of Python and also the virtual environment(if configured) as shown below.

image 1

Python Interpreter

Solution 3 – Installing psutil inside the virtual environment

Many different IDEs like Jupyter Notebook, Spyder, Anaconda, or PyCharm often install their own virtual environment of Python to keep things clean and separated from your global Python.

If you are using VS Code, then you can also create a virtual environment, as shown below.

In the case of virtual environments, you need to ensure that the psutil module needs to be installed inside the virtual environment and not globally.

Step 1: Create a Virtual Environment. If you have already created a virtual environment, then proceed to step 2.

Step 2: Activate the Virtual Environment

Step 3: Install the required module using the pip install command

# Create a virtual Environment
py -3 -m venv venv

# Activate the virtual environment (windows command)
venv\Scripts\activate.bat

# Activate the virtual environment (windows powershell)
venv\Scripts\Activate.ps1

# Activate the virtual environment (Linux)
source venv/bin/activate

# Install psutil inside the virtual environment
pip install psutil

Solution 4 – Ensure that a module name is not declared name a variable name.

Last but not least, you may need to cross-check and ensure that you haven’t declared a variable with the same name as the module name.

You should check if you haven’t named any files as psutil.py as it may shadow the original psutil module.

If the issue is still not solved, you can try removing the package and installing it once again, restart the IDE, and check the paths to ensure that packages are installed in the correct environment path and Python version.

Conclusion

The ModuleNotFoundError: No module named ‘psutil’ error occurs when we try to import the ‘psutil‘ module without installing the package or if you have not installed it in the correct environment.

We can resolve the issue by installing the psutil module by running the pip install psutil command. Also, ensure that the module is installed in the proper environment in case you use any virtual environments, and the Python version is appropriately set in the IDE that you are running the code.

August 06, 2022 07:51 PM UTC