Home  >  Q&A  >  body text

python3.x - Why can't python find the module even after setting the .pth file?

My python is version 3.5.2.
I have a helloworld.py module, which has a show function to output "Hello World". I put this module under the pck folder, and then put the pck folder under the C drive.

Then, I created the PckPath.pth file in the site-packages directory of the python installation path, and tried various writing methods:

No matter which one it is, create a new test.py file on the desktop and enter

import pck.helloworld
pck.helloworld.show()

It will prompt in IDLE

Traceback (most recent call last):
  File "C:\Users\Berlin\Desktop\test.py", line 1, in <module>
    import pck.helloworld
ImportError: No module named 'pck

Why? ? If I set sys.path.append(...) directly in test.py, it can be used normally. What is the problem? How to get the .pth file?

伊谢尔伦伊谢尔伦2646 days ago960

reply all(2)I'll reply

  • 黄舟

    黄舟2017-06-28 09:27:47

    The default places where Python looks for packages are as follows:

    1. The site-packages directory

    2. under the Python installation directory
    3. Environment variable PYTHONPATHdirectory

    4. Current directory

    5. sys.path.append(...)Added directory (this is temporary)
      However, if you only put the package under the C drive, then it does not belong to any of the situations. Of course, Python will find it. No more bags. In addition to the sys.path.append() method, it is also possible to add C: to the PYTHONPATH environment variable in the above method.

    reply
    0
  • PHP中文网

    PHP中文网2017-06-28 09:27:47

    Any language is almost always in this order from system variables, language setting variables to the final real-time recorded variables. For the same variable, the latest one is effective. This is how I check the environment for learning these languages ​​and so on, and I’ll be sure of everything

    reply
    0
  • Cancelreply