简体   繁体   中英

Python modules' paths

In some Python scripts I see the following imports:

import fileA
import someDir.fileB
from fileC import functionA

There exist corresponding files fileA.py , someDir/fileB.py and fileC.py . However, while looking in the Requests source code, I found this in the __init__.py file:

from requests.packages.urllib3.contrib import pyopenssl

In this case, requests is the CWD and packages.urllib3.contrib.pyopenssl.py is the file. Why does this defy convention? I do see that the packages.urllib3.contrib directory does also have a __init__.py file, which seems to be related .

Furthermore, I'm not sure if it is related but I think it is so I post it here. In my script I have the folder kennethreitz/requests , since the application depends on the Requests module but I'm deploying it to environments which might not have Requests installed. However, simply adding to the file import kennethreitz.requests is not including the Requests module. I import kennethreitz.requests.__init__ and a few other obvious permutations but I cannot get the module to import. How can I package Requests with my code? The obvious Google searches are not helping.

requests is using an absolute import. You cannot arbitrarily nest packages into other directories and still expect things to work.

Instead, add the kennethreitz directory (which should not have a __init__.py file) to your sys.path module search path. That way the requests module will still be importable as a top-level package.

Next, you may want to look into Python packaging, dependencies and using a tool like pip or zc.buildout to deploy your code for you. Those tools handle dependencies for you and will install requests as required. See the Python Packaging User Guide for an introduction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM