Resolve Python failed to import package at CMD runtime, error message "Modulenotfounderror:no module named * * * * * *"

Source: Internet
Author: User

1. For my automated test engineering structure diagram

I started my automated test by running the Run.bat batch file, calling the cmd console to run start_run.py, but the following error occurred:

You may know that our project runs in the IDE (PYCHARM) and we run in CMD in a different way, when running in Pycharm,

The default Pycharm directory + Our project directory is the running directory, and when run in CMD, it will run in the directory where our catalog is located.

In the import package, the package is first viewed from the PYTHONPATH environment variable, and if none of the directories contained in your Pythonpath are

The root directory of the project directory, you will get an import error when you import a package in another project that is not in the same directory

Workaround: Add your project directory to Pythonpath in the file you are running, as follows:

Import Os,sys

Sys.path.append ("path")//path is the absolute path to your project root directory

If you still get an error while running, check to see if the root directory is added to Pythonpath by:

Import OS

PATH = Os.environ

For key in PATH:

Print (Key,path[key])

If the directory contains the project root you are in, then check to see if all of your directories contain __init__ files.

When we import a module under another package, we first need to determine if the package is a Python package, and the __init__ file is

To prove that this directory is a python package directory!

While I was doing something online, I found that I still failed, and I found a __init__ file in my root directory as well.

And I deleted the file, run everything normal, I do not know the specific reason, feel a bit strange!

Resolve Python failed to import package at CMD runtime, error message "Modulenotfounderror:no module named * * * * * *"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.