Package installed but doesn't import package - python

For some odd reason no matter which package I install when I go to import it doesn't know what package I'm talking about. I am very certain this is a Visual Studio Code error but if not I am also using Linux.
When I pip install the package pyttsx3 this is what I get in the Terminal:
Collecting pyttsx3
Downloading https://files.pythonhosted.org/packages/24/4e/580726c73272344d3e74b7aaffae55ff6b6450061fbecb8cc6e112531c02/pyttsx3-2.7.tar.gz
Building wheels for collected packages: pyttsx3
Running setup.py bdist_wheel for pyttsx3 ... done
Stored in directory: /home/secretlloyd/.cache/pip/wheels/a2/8a/fe/11112aca9c89142c3a404bc67ef3393a7ad530da26639a05d4
Successfully built pyttsx3
Installing collected packages: pyttsx3
Successfully installed pyttsx3-2.7
But when I run a example I get this error:
Traceback (most recent call last):
File "/home/secretlloyd/Visual Studio Code/Python/Finished/Text Colors/finished.py", line 1, in <module>
import pyttsx3
ModuleNotFoundError: No module named 'pyttsx3'

You can use an virtual environment to install your libs. If you do that, each project will have its own scoped libs without affect your global libs.
How to use the virtual environment?
Enter the root folder of your project and then run the following commands on the bash:
$ py -m venv .env
$ source .env/Scripts/activate
After that you'll notice your bash will have a prefix like that (.env). Then you should install your libs:
(.env) $ pip install pyttsx3
In order to deactivate the virtual environment just run the following command:
(.env) $ deactivate
Setup VS Code Intellisense for Virtual Environment
If you're using VSCode you can set the correct python interpreter after setting up a virtual environment. Just follow the steps:
Open VSCode in your project
Press F1
Type: > python: select interpreter
Click on Enter path or find an existing interpreter
Click on Find
The navigate to .env > Scripts > python

3 possible cases:
The same thing happened to me when I did not notice I was using two Pythons at the same time one 2.7 and another one 3.6. Make sure to know where is your package being installed to the Python modules folder you really want to store it or in another one you did not know existed.
Your PATH might not be configured correctly, check out either if you are using Windows or Linux if your PATH variables are configured correctly. You can reset your configuration if you wish. (link= How to reload .bashrc settings without logging out and back in again?)
For some packages/libraries of Python the way of importing the library is different from the name you import it on your .py file. For example: You can install OpenCV library by [pip install OpenCV] but when importing it in a file you have to write [import cv2].
I hope you find this information helpful for your problem.

Related

Unable to move the Stable Diffusion pipeline to my M1 MacBook

I am following the steps stated here: How to use Stable Diffusion in Apple Silicon (M1/M2).
At my local MacBook M1 machine, I saved the below script in stable-diffusion.py file:
# make sure you're logged in with `huggingface-cli login`
from diffusers import StableDiffusionPipeline
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
pipe = pipe.to("mps")
# Recommended if your computer has < 64 GB of RAM
pipe.enable_attention_slicing()
prompt = "a photo of an astronaut riding a horse on mars"
# First-time "warmup" pass (see explanation above)
_ = pipe(prompt, num_inference_steps=1)
# Results match those from the CPU device after the warmup pass.
image = pipe(prompt).images[0]
Now when I am trying to execute: python stable-diffusion.py from Terminal, I am getting following error:
Traceback (most recent call last):
File "/Users/apple/Desktop/area_51/stable-diffusion.py", line 2, in <module>
from diffusers import StableDiffusionPipeline
ModuleNotFoundError: No module named 'diffusers'
In order to fix it even I tried: pip install diffusers, however I still got same error.
Am I missing anything over here?
If you have already installed diffusers but are still encountering the ModuleNotFoundError, it's possible that the module is installed in a different Python environment than the one you are running your script from. In that case, you may need to check your Python environment settings and ensure that the module is installed in the correct environment.
To check your Python environment settings, you can use the following steps:
First, determine which Python interpreter you are currently using by running the following command in your terminal:
which python3
This should output the path to the Python interpreter that is currently being used. (I assume you're using python3. If you are using python2 for some ungodly reason, you should switch to python3.)
Next, ensure that the diffusers package is installed in the environment associated with the Python interpreter you are using. You can do this by running the following command:
python3 -m site
This will output information about the Python installation, including the location of the site-packages directory where installed packages are stored.
Look for a line that says "sys.path" or "USER_SITE" to find the location of the site-packages directory. This is the directory where Python looks for installed packages.
Check if the diffusers package is installed in the site-packages directory. You can do this by looking for a directory called diffusers inside the site-packages directory.
For example, if the site-packages directory is located at /usr/local/lib/python3.9/site-packages, you can check for the diffusers package by running the following command:
ls /usr/local/lib/python3.9/site-packages | grep diffusers
If the diffusers package is installed, this command should output a directory called diffusers. If the package is not installed, the command will not output anything.
If the diffusers package is not installed in the correct environment, you can try installing it using the appropriate package manager for that environment. For example, if you are using a conda environment, you can try installing the package using conda (conda install -c conda-forge diffusers). If you are using a virtual environment created with venv, you can try activating the environment and installing the package using pip (pip3 install diffusers).

Visual Studio Code is rejecting the Tensorflow installation in a Virtual Environment

I created a virtual environment called env using
python -m venv env
.\env\Scripts\activate.bat
pip install tensorflow
I verified tensorflow is in the env\Lib\site-packages folder
Next I loaded VS Code and created a workspace, added a python file, it prompted me to install pylint,
I typed in python: select interpreter and I browsed to C:\Users\admin\env\Scripts folder
This is the command line at the beginning of the script
(env) PS C:\Users\admin\env\project> cd 'c:\Users\admin\env\project'; & 'C:\Users\admin\env\Scripts\python.exe' 'c:\Users\admin\.vscode\extensions\ms-python.python-2020.8.106424\pythonFiles\lib\python\debugpy\launcher' '54436' '--' 'c:\Users\admin\env\project\face_gan.py'
This is the error I get when debugging the python file:
ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via `pip install tensorflow`
PS C:\Users\admin\env\project> & C:/Users/admin/env/Scripts/Activate.ps1
When I type in pip install tensorflow in VS Code terminal, it shows its already installed
(env) PS C:\Users\admin\env\project> pip install tensorflow
Requirement already satisfied: tensorflow in c:\users\admin\env\lib\site-packages (2.3.0)
I don't understand this, is it not running in virtual environment?
Why is it executing C:/Users/admin/env/Scripts/Activate.ps1 at the end of the debugging session, not at the beginning
Lastly, is running python from the virtual environment folder C:\Users\admin\env\Scripts the same as using the activate.bat file or the source command? Does it automatically defer to using the C:\Users\admin\env\Lib folder, or is it still trying to use the default python installation to look for Tensorflow?
What step did I miss to make it use the virtual environment correct in VS Code?
First question: executing C:/Users/admin/env/Scripts/Activate.ps1 after debugging command make no difference. It just because it's the first command of the terminal. You can run it again to make a try.
Second question: Yes, that's the same. In your case, it will add 'C:\Users\admin\env' and 'C:\Users\admin\env\lib\site-packages' path to the PYTHONPATH variable.
You can through these codes to get the PYTHONPATH(the default search path for module files) variable value:
import sys
print(sys.path)
If you import 'tensorflow' directly. you will find you can import it correctly. It's a version problem. You should downgrade the version of the packages, and you can refer to this comment to get some useful information.

Getting the error "from robot import run_cli ImportError: No module named robot" even when robotframework was installed in the system using python?

While running the command pybot --version
I'm getting the error
from robot import run_cli ImportError: No module named robot
I have already installed robotframework 3.0 with python after downloading the module with its setup.py file.
I tried installing and reinstalling it multiple times.
Also I have verified the environment variables for the same which also seems to be inline with what I have installed.
I checked in the site-packages also where I am able to see robotframework 3.0 present in them.
I checked in the /usr/local/bin as well as /home/.local/bin folder I can see both robot and pybot available. But for running the command robot --version also it is showing the same error.
I really don't know what is missing.
My Environment:
Ubuntu 16.04
python 2.76
robotframework 3.0
Thanks in Advance!
I have little experience on installing the ROBOT Framework in linux machine. But just check whether you have done the following:
How did you install ROBOT Framework? Is it by pip command or with the downloaded source file? Have you tried with pip command if any?
Set python path in your environment path/variables. Example in windows, C:\Python27\
Set python scripts folder in your environment path/variables. Example in windows C:\Python27\Scripts
Last, maybe you can share the output of your 'pip list' command? So, just want to see what are the modules/packages that you have installed.

Unable to use azure SDK in Python

I did this:
sudo pip install azure azure-storage azure-servicebus azure-mgmt azure-servicemanagement-legacy
from azure import *
Traceback (most recent call last): File "", line 1, in
ImportError: No module named azure
from azure.storage import BlobService
Traceback (most recent call last): File "", line 1, in
ImportError: No module named azure.storage
Python package installed thru cmd sudo pip install exists at the paths /usr/lib/python2.7, /usr/local/python2.7, etc and their sub-folder dist-packages.
You can code import sys and print sys.path in the Python Interpreter to show the completed path list for current python environment.
Iif you installed successfully some packages like azure & azure-storage, you can find these files relate to the package in the python library paths.
However, you got the error in Import Error: No module named <package-name> when you run the code import <package-name> or from <package-name> import <class or object name>. There are two scenes that would be cause the issue normally.
Package not installed successfully.
The library path included package that not exists in Python system environment path sys.path in python or PYTHONHOME in environment variables.
So I think you can try to solve the issue thru three ways below.
Dynamically add the package path into sys.path thru the method sys.path.append('<package path>') in python code.
Check the environment variable PYTHONHOME whether or not set up. If set up PYTHONHOME, python will add package path based on PYTHONHOME into sys.path.
If your python environment encounter some unknown fault that is unrecoverable, you can try to reinstall Python thru commands sudo apt-get remove python python-pip, sudo apt-get update, sudo apt-get install python python-pip on Ubuntu. It's a simple way.
BlobService belongs to azure.storage.blob rather than the azure.storage
it should rather be
from azure.storage.blob import BlobService
Link - https://azure.microsoft.com/en-us/documentation/articles/storage-python-how-to-use-blob-storage/
If it still doesn't work for you, you might would like to use virtualEnv and do the pip install again while in virtualenv
http://docs.python-guide.org/en/latest/dev/virtualenvs/
I had very similar issue. There was a lot of confusion between python2 and python3 package versions as there was no virtual env used and I also had to ungrade pip to 18.
But anyway, this is is how I resolved the part in question.
Locate where the package was installed:
pip show azure
The output will show the location of the package in the Location section:
Name: azure
Version: 4.0.0
Summary: Microsoft Azure Client Libraries for Python
Home-page: https://github.com/Azure/azure-sdk-for-python
Author: Microsoft Corporation
Author-email: azpysdkhelp#microsoft.com
License: MIT License
Location: /usr/local/lib/python3.6/dist-packages
Requires: azure-servicefabric, azure-cosmosdb-table, azure-datalake-store, azure-loganalytics, azure-eventgrid, azure-servicemanagement-legacy, azure-servicebus, azure-graphrbac, azure-storage-blob, azure-mgmt, azure-storage-file, azure-batch, azure-applicationinsights, azure-keyvault, azure-storage-queue
Required-by:
If you do:
python -c "import sys;print(sys.path)"
You will see a list of pip package locations:
['/app', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages']
At the begining of my python file I added:
sys.path.insert( 0, '/usr/local/lib/python3.6/dist-packages' )
This will make sure this package location will be checked in the first place.
UPDATE
Thinking about it in the morning, things opened from a new perspective for me. I saw, that I had #!/usr/bin/python at the beginning of my python file, which says to use the wrong interpreter and look for pip packages in the wrong place.
azure metapackage is deprecated
and azure-storage is not being maintained anymore
Please use azure-storage-blob >= 12.0
pip install azure-storage-blob
from azure.storage.blob import BlobServiceClient
PS: I write SDKs for azure

Installing GDAL Python binding in Ubuntu to use as standalone module

I am trying to export a GeoTiff with Blender using the Blender Python API (based on Python 3), so I've decided to install GDAL on Ubuntu (14.04). What I would like is to get the module as a standalone folder that I could put in the modules directory of Blender (/home/user/.config/blender/2.73/scripts/modules).
The thing is I've run through several different problems trying to install GDAL. I've tried to install from source (for GDAL 2.0.0) here : Official PyPi Gdal
I ran sudo apt-get install libgdal-dev gdal-bin (I list it here because it may be important)
When I am in the extracted GDAL folder, using python setup.py build & python setup.py install, the library installs to /usr/local/lib/python2.7/dist-packages/osgeo. However, when I run python from command line, running from osgeo import osr returns ImportError: No module named _gdal
Following GDAL via pip , I used pip (pip install GDAL) to install the library, and the folder it went to was /usr/lib/python3/dist-packages/osgeo (using pip show ...). Again, running python3 and trying to import results in the same error. Of course, when I copy-paste each folder in the blender module directory, I get the same error in the Blender Python console.
So I decided to compile the sources using ./configure --with-python & make & make install in the source folder. I then copied the folder GDAL-x.x.x/build/lib.linux-x86_64-3.4/osgeo to the blender modules directory and got this time the error when importing : ImportError: /home/yvesu/.config/blender/2.73/scripts/modules/osgeo/_gdal.so: undefined symbol: _Py_ZeroStruct.
Trying to compile with python3 using python3 setup.py build returns the error error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
EDIT 1:
I think I've found the solution : I went to the directory swig/python (not found in a GDAL-1.11.0 folder but gdal-1.11.0 fodler, can't remember where I downloaded it from), ran python3 setup.py build & python3 setup.py install and could finally find the folder in /usr/local/lib/python3.4/dist-packages/GDAL-1.11.0-py3.4-linux-x86_64.egg/osgeo. When I put this osgeo folder oni the Blender modules directory, I was able to import osgeo in Blender. I will report if anything went wrong.
I think I've listed all my attempts at installing GDAL on Ubuntu. Can anyone point me in the right direction? Do you think it is even possible to install it as a standalone module, or do I need linked libraries through LD_LIBRARY_PATH?
Here is the solution I've found :
Download Gdal sources (v2.0.0 is the current stable release) from ftp://ftp.remotesensing.org/gdal/2.0.0/ or http://download.osgeo.org/gdal/2.0.0/ and untar
Go to the directory gdal2.0.0/swig/python
Run python3 setup.py build & python3 setup.py install
Finally find the module folder in, on Ubuntu : /usr/local/lib/python3.4/dist-packages/GDAL-2.0.0-py3.4-linux-x86_64.egg/osgeo
I can now use it in Blender (copying in the modules directory)

Categories