garg10may
garg10may

Reputation: 6179

Import Python module (from another path) and use as default

I want to import requests module which is located at another path. I want to use below code. Also I could see the requests directory where the module is installed has bunch of other files. What are this needed for.

>>> r = requests.get('https://api.github.com/user', auth=('user', 'pass'))
>>> r.status_code
200

Below are some of the contents of /Requests dir.

adapters.py
adapters.pyc
api.py
__init__.py

......

Below question has some great answer, but still confuses me.

How to import a module given the full path?

Upvotes: 0

Views: 626

Answers (2)

User
User

Reputation: 483

No you don't have to include the .py file. Python interpretor looks at the path mentioned for init.py file. If it's there the code should work fine. Though for Python 3 init.py file is also not required.

As I can see in the directory listing of yours init.py file is there. Also note init.pyc file is there, it's just the cache version of above.

To see all the paths that interpretor would be referring to do below

import sys
sys.path 

What you are using here is

sys.path.append('/.../requests/')

Don't use the extra '/' after the requests, it means it's looking for requests directory in requests folder and gives you the error No module named requests.

Upvotes: 1

Grysik
Grysik

Reputation: 846

Doesn't simple

import sys
sys.path.append('path\\to\\request_module')

solve your problem?

Upvotes: 0

Related Questions