How does one fix an import error like the following?

This code:

from azure.cognitiveservices.vision.customvision.training import CustomVisionTrainingClient
from azure.cognitiveservices.vision.customvision.training.models import ImageFileCreateBatch, ImageFileCreateEntry, Region
from msrest.authentication import ApiKeyCredentials
import time
import os

def main():
    from dotenv import load_dotenv
    global training_client
    global custom_vision_project

Errors generated:

"message": "Import \"azure.cognitiveservices.vision.customvision.training\" could not be resolved"

You have aready ask this question and have an answer.
Why are you repeating it?
Previous question: Import Error where I believe the sample program is trying to access a web page

1 Like

It does seem like the same problem. I assume the fix the OP reported there was a false diagnosis and starting over is clearer.

@gerberw : do you understand that the Python import mechanism doesn’t download from the Internet (not normally, anyway) and that you have to install packages first, before referring to them in your program? Like this:

Python is often run (by your IDE, for example) in a virtual environment. These are useful for isolating what you install for a particular project from other projects and your main installation. By the same property, they can be confusing because you may have installed something in one environment, but now your IDE is using another where it doesn’t exist. If you are using some kind of hosted environment to run your code, you need to figure out how that works there.

The error message you post isn’t what Python normally says when it cannot resolve an import. I get something like:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'zob' from 'os' (...\Python311\Lib\os.py)

This makes me think you are seeing this through some on-line environment where you are not fully in control of installations, or it is done some other way. You can still ask it where it is looking.

The places Python looks for packages to import are in sys.path. Mine looks like this on Windows (when I replace the personal directory stuff with ...):

>>> import sys
>>> sys.path
['', '...\\Python311\\python311.zip', '...\\Python311\\DLLs', '...\\Python311\\Lib', '...\\Python311', '...\\Python311\\Lib\\site-packages']

Lastly, if you are posting code or error messages, highlight what you pasted and press the button that looks like </> in the message editor. That way , you preserve the indenting and make it more readable for people trying to help.

Thank you for your response. Just to give you some background, I am using a training module in Azure to learn how to use AI in Azure. You can use a virtual environment or use it on your PC. They give the instructions on how to use your PC, where you can use C# or Python. They generate the application code. They provide you with the instructions on loading any needed components to do this.
What seems to be happening, even though the path does contain the correct subdirectory where the needed Azure components reside, the path prior to the subdirectory is incorrect. I have reviewed the various env files, checked the path and even gone so far as to look at regedit to see where the path can be changed. I have run out of ideas. I am new at this so sorry for displaying the code incorrectly. Writing this I realize that it is not a Python but a Windows environment problem which I have also not been able to fix.

Hi Wolfgang.

How are you running this code on your PC? With the python command at the prompt? In an IDE? Or indirectly through something Azure gave you?

Are you familiar with running simple Python programs on your PC that you have written yourself?

When you write:

what path are we talking about? The Windows environment PATH? Or sys.path? Or each directory path in this list? The latter is what determines where Python looks. The former will determine only where it looks for python.exe, although Python will construct sys.path relative to its own location.

Could you edit into your code before the imports the fragment:

import sys
print(sys.path)

This will tell you where Python is looking for modules. Things you have installed with pipwill be in the directory that ends site-packages. Are they in there? Are you maybe running a different Python from the one you installed azure into?

If you’re still stuck, tell us what it prints. (You can take out the personal parts of the directory paths if you like.)

The imports work for me if I do this.

Let’s start with an environment in which Python works normally. I’m using the PowerShell command prompt. Let’s check:

PS gerberw> get-command python

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     python.exe                                         3.11.51... C:\Users\Jeff\AppData\Local\Programs\Python\Python311\python.exe


PS gerberw> python -V
Python 3.11.5
PS gerberw> python -c "print('Hello world!')"
Hello world!

I do this next bit because I don’t want to install a lot of Azure code in my system Python. It makes a virtual environment (a sort of project-specific installation of Python). It is optional, but is good practice when installing dependencies. It changes the prompt to remind you.

PS gerberw> python -m venv .\venv
PS gerberw> .\venv\Scripts\activate
(venv) PS gerberw> get-command python

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     python.exe                                         3.11.51... C:\Users\Jeff\Documents\ML\gerberw\venv\Scripts\python.exe

Notice that Python (some of it) is now in the project local directory venv, and that is on my Windows path.

Now we can get your code into a program. I will use IDLE the simple IDE that comes with Python. I’ll use pythonw to spin it off so I get my prompt back.

(venv) PS gerberw> pythonw -m idlelib.idle imptest.py

There are nicer ways to launch IDLE but it depends how your PC is set up. That one ought to work anywhere.

In the editor I enter enough of your code to test the import statements:

from azure.cognitiveservices.vision.customvision.training import CustomVisionTrainingClient
from azure.cognitiveservices.vision.customvision.training.models import ImageFileCreateBatch, ImageFileCreateEntry, Region
from msrest.authentication import ApiKeyCredentials
import time
import os

then I save (ctrl-S) and run (F5). This happens in a new window:

Python 3.11.5 (tags/v3.11.5:cce6ba9, Aug 24 2023, 14:38:34) [MSC v.1936 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.

= RESTART: C:\Users\Jeff\Documents\ML\gerberw\imptest.py
Traceback (most recent call last):
  File "C:\Users\Jeff\Documents\ML\gerberw\imptest.py", line 1, in <module>
    from azure.cognitiveservices.vision.customvision.training import CustomVisionTrainingClient
ModuleNotFoundError: No module named 'azure'

which is the sort of message I expected. This is because we haven’t installed the Azure modules.

Back in the command prompt I install what it needs:

(venv) PS gerberw> pip install azure-cognitiveservices-vision-customvision
...

When I now re-run the code in IDLE the imports execute perfectly well. (Nothing to see.)

Each time you need to start a new command shell in this directory you will have to do the .\venv\Scripts\activate bit again, but not anything else. What you installed is still there. You can see it in venv\Lib\site-packages.

Thanks Jeff,

I am running this in Visual Studio Code as my IDE. I have checked the sys.path and it looks like it should be able to find the azure subdirectory.

Running Python 3.9.9.

When I carry out what you describe everything works fine. Thank you!

However, when I run the full program I now get this error:
Traceback (most recent call last):
File “C:\Users\gerberw\imptest.py”, line 55, in
main()
File “C:\Users\gerberw\imptest.py”, line 8, in main
from dotenv import load_dotenv
ModuleNotFoundError: No module named ‘dotenv’

Obviously there is something else going on. Using File manager search I cannot find this file anywhere.

It’s good news that that works.

If it works at the prompt, this begins to sound like a problem with the way VSCode is setting up the environment in which it runs your code. I don’t know VSCode in any detail.

In PyCharm, for a given project, I have to choose which installation of Python to use, and whether to have a virtual environment (usually yes). It manages the installation of dependencies (the pip bit) somewhat automatically. However, it is good to go through the process manually, as I showed it, to understand what an IDE is doing for you or asking of you. I assume VSCode has equivalent functionality.

Your solution for that particular error when running at the prompt is to install the missing module: python-dotenv · PyPI using pip. In the IDE, VSCode will need to be instructed (or at least guided) what dependencies to get.

I notice from the traceback you post that you appear to be working directly in your home/personal directory. Normally one would create a subdirectory for each project and keep related files there, away from other files. An IDE will expect that kind of set-up and create its own files there to describe the particular project. It may even be the reason VSCode is behaving unhelpfully. Start over in a nice, empty directory you can point VSCode at.

Thanks for the help Jeff. Is there a source that you could recommend that would teach me how to set up a Python environment for an application that uses different libraries, for example, math and plotting libraries? I have used other IDE’s that come with Anaconda plus I have a beginners’ Python coding book which do not do a very good job of explaining how to create environments for various applications. What would also be helpful is how to use Github in an application.

This particular application gives the instructions to set up the environment and utilizes Github cloning but obviously something went wrong. So, without understanding how the Python environment works in general it is nearly impossible for me to troubleshoot the problem. And yes, I have had issues with getting examples not running correctly when practicing coding.

Thanks again.

Hi - I feel tempted to editorialize about (@#$!) Python primers that don’t explain clearly how to set up Python virtual environments, but since that will not help you, I will restrain myself :slight_smile:

Main thing to know about a Python virtual environment is just that it is a local directory tree, containing the Python interpreter plus all packages (including all third-party packages that you install separately). Installing mainly means unpacking packages as subdirectories inside the site-packages directory.
You can create multiple envs (for different Python versions or with different sets of 3d-party packages) on the same host machine. The environment basically just is (the path to) a particular Python interpreter plus its set of libraries (which are found at standard subdir locations, if you don’t override the OS environment variable PYTHONPATH).

Documentation about how to set this up: venv — Creation of virtual environments — Python 3.12.0 documentation
Alternatively, you can also use the conda command to work with packages released through anaconda.

I don’t know if VSCode directly supports setting up a Python virt. env, but it’s definitely possible to just set one up outside of VSCode, then activate it, and then edit/debug files inside VSCode.

1 Like

It does according to this tutorial and this more detailed help.

@gerberw : as Hans has suggested the standard documentation on venv is a good place to start (although you won’t need most of it) and I would add basic pip commands. When you play with pip, use a virtual environment because then you can make as much mess as you like and see what is happening in site-packages. Python is pretty up front about what it is doing.

VSCode promises to help you do this, and thankfully seems not to hide the mechanics from you.

I use GitHub through the git command at the prompt. I use the IDE to choose files and write a commit message and commit to the local repository. But when it comes to push and pull, to manipulating branches and remotes, I prefer the command line.

The git book is very good, and you can usually find a command to adapt by searching (often on StackOverflow). An IDE adds a layer, sometimes with its own concepts. I always feel it might do something unexpected, which is annoying if it is local and very annoying if it has been published at GitHub.

1 Like

Thank you very much for your enlightening comments, Han and Jeff. What are your thoughts on using Anaconda? Looking into the application it appears it takes care of a lot of the necessary Python infrastructure.

I will look into your recommended reading list, thanks.

Conda is a full-fledged package and environment manager (not just for Python). It’s simple to use and it guarantees that packages installed (with conda) are all version-compatible (the env as a whole will remain consistent). Using just pip this will not always be guaranteed. Conda installs use pre-built wheels always - which means that installs of big packages like pytorch or tensorflow, and many other packages that contain extension modules, will not fail in the build phase (for any number of reasons). So, people who work in data-science or in ML generally love working with conda - I certainly do :slight_smile:

Main caveat when working with conda is that if you use it, you need to be careful to only use pip when a package is not available on the anaconda repositories, because pip doesn’t know about the conda meta-data (metadata used during installs to guarantee that the env remains consistent) so this can lead to inconsistenties between the installed packages in an environment.
See: Anaconda | Understanding Conda and Pip

2 Likes

Thanks for the very helpful and useful information. I am trying to learn python for data science and ML. Getting very frustrated with the environment stuff. Thought that learning through Microsoft training utilizing Azure would be the key. However, that has not been the case. Have to do a reset on where to go from here. Although Anaconda does support the Microsoft VS code environment. Maybe I can continue the Microsoft training examples, getting them to work using the Anaconda structure.