-1

I have downloaded a module and moved it to my working directory, how can I import this module and all of its contents to use on my code?

a view of my working directory: E:\myprojects\projectA

location of the downloaded module: E:\myprojects\projectA\tweepy

NOTE: I tried installing it using a CMD and I was unable to that's why I want to do it this way instead.

HumDum
  • 23
  • 6
  • [Everything you ever wanted to know about importing](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time?noredirect=1&lq=1) There is no simple answer. Pythin import is not intuitive – Francis Cagney Feb 13 '22 at 22:59

2 Answers2

1

You need to set the file path manually to import the modules from another directory. You can assign a directory path to the PYTHONPATH variable and still get your program working.

In Linux, you can use the following command in the terminal to set the path:

export PYTHONPATH='E:\myprojects\projectA\tweepy'

In Windows system :

SET PYTHONPATH='E:\myprojects\projectA\tweepy'

To see if PYTHONPATH variable holds the path of the new folder, you can use the following command:

echo $PYTHONPATH

Then you can do your imports:

import tweepy
#import tweepy as tw
#from tweepy import sth
Obaskly
  • 477
  • 2
  • 13
  • wouldn't that affect me later? when i try to import any installed modules as the path wouldn't be in the Lib directory any more? – HumDum Feb 13 '22 at 23:29
  • 1
    The commands above add the custom module directory in PYTHONPATH environment variable, which will augment the default module search paths used by the Python interpreter. – Obaskly Feb 14 '22 at 00:26
0

Normally to import a module you need to install it first, not just download it. In almost every case, your IDE will help you to install it.
Once installed, all you need to do in your code is to add this line at the beginning of it:

import tweepy as name

Where name is the name you would like to use for using that module functions.

FourBars
  • 475
  • 2
  • 14
  • I tried doing so, but for some reason module is not found every time I try to import it that way, you can check the question i posted yesterday, sadly nobody was able to assist me on my matter – HumDum Feb 13 '22 at 23:28
  • I assume you're using `pip`, if you don't, you can learn how to install it [here](https://www.geeksforgeeks.org/how-to-install-pip-on-windows/#:~:text=Download%20and%20Install%20pip%3A&text=Download%20the%20get%2Dpip.py,where%20the%20above%20file%20exists.&text=and%20wait%20through%20the%20installation,now%20installed%20on%20your%20system.). Oce you did it and you're sure it's working well, you just need to run `pip install tweepy`, and that should solve your problem. – FourBars Feb 14 '22 at 08:29
  • Hello @HumDum, I can see you tried to install it from CMD with no good results. Are you sure you have `pip` properly installed on your computer? – FourBars Feb 15 '22 at 09:52