About the Python project path import your own Write library error

Source: Internet
Author: User

In fact, it is also in writing their own project when encountered, but has also encountered but has been to take the strategy of avoidance, this time finally make it clear so summed up.

The top-level directory for this project is Medivac, which is itself a Python module.

People familiar with flask know that when writing a flask project, we often initialize a module's app to the __init__.py file in the root directory.

It is then convenient to introduce other modules that need to be routed into this __init__.py to automatically load and route the effect.

Alternatively, you can use a thing called Flask Blueprint (Blueprint) to subdivide the module.

If the app was initialized in __init__.py (temporarily understood as App=flask (__name__)), name in the views we create a file called Hello, and write a function to route him like this

 from Import App@app.route ("/") def Hello ():     return " Let ' s move! "

Would make such a mistake.

 from Import appimporterror:no module named Medivac

Why can't I find the Medivac module? We all know that it is possible to write the module name directly when you import the module under __init__.py, because __init__.py will run the load by default. If you use the IDE you will find that the code is no problem and can jump normally.

Later found that can not be written so, must be medivac above and the folder to live, such an address can be. In Python's view, he doesn't see the root directory, and he thinks the project directory is

medivac/, not/medivac/.

So if you change it,

 from __init__ Import App

can run properly.

But I think it's a lot of holes. A. py file to initialize the app and then use the blueprint to route it is also a good choice.

In particular, there is an environment variable pythonpath that seriously affects the addressing of the module, which sets the pythonpath of the current environment variable to the corresponding address, which can be conveniently used to help with absolute addressing.

In addition, by the way, about the configuration of Uwsgi

[Uwsgi] # Uwsgi the address and port used when booting socket = 127.0.0.1:8001#  point to the site Directory chdir =/home/medivac/project/  Medivac#  Application variable name in Python program to start module == = True#  number of processors processes = 1#  threads threads = 1#  worker process workers = 1

When the specified chdir is/home/medivac/project/medivac, then the module represents the file to which to find the callable app to load.

A little thought about how the Python project path imports its own library error

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.