Is it possible to use a module without installing it on your computer?

12,290

Solution 1

You're looking for sys.path which will do exactly what you want.

When you try to load a module, Python searches in the current directory for a module with that name. If it doesn't find something there, it searches other places in some order and if it isn't found anywhere it is allowed to look, it'll raise an ImportError. By adding your path to the folder on your USB where you have a version of the module, it should work.

import sys
sys.path.append("/path/to/your/folder")

import selenium

You can also print sys.path to see in which directories Python searches for modules.

Solution 2

Beside modification sys.path in a script, you can use PYTHONPATH environment variable. According to The Module Search Path in documentation:

sys.path is initialized from these locations:

  • the directory containing the input script (or the current directory).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • the installation-dependent default.

On Windows for example:

set PYTHONPATH=c:\path\to\modules;d:\another\path
python script.py

On Linux for example:

export PYTHONPATH=/path/to/modules:/another/path
./script.py

Solution 3

Yes it is possible and there are a couple ways to do it.

One way would be to place it in the same directory as the script and import it.

Another way would be to set your system path:

import sys

script_path = "path/to/selenium"

if script_path in sys.path:
    print "oops, it's already in there."
else:
    sys.path.insert(0, script_path)

# proceed to import selenium here
Share:
12,290

Related videos on Youtube

PeepingHog
Author by

PeepingHog

Updated on September 15, 2022

Comments

  • PeepingHog
    PeepingHog over 1 year

    For example, my college doesn't give students administrator privileges so I'm unable to install selenium webdriver. The python code that I typed does not work. Is it possible for me to use selenium without having it installed on the computer? Are there any alternatives? Can I use a usb/thumb drive with selenium in it and have the code run it through that drive?

    • sabbahillel
      sabbahillel about 8 years
      You should first verify that you are allowed to do this. Many places have restrictions on using what is in a USB and what can be attached to the system because of viruses and malware. Do not put your career in jeopardy. If it is something for school that needs Selenium, (or even if it is work on your own) go through a professor to get it installed.
  • embe
    embe over 5 years
    Do you have to do anything else with the module before you can import it? In my case I have downloaded tabula-py.zip from github and unzipped it to a folder called lib inside current directory. Getting ModuleNotFoundError
  • embe
    embe over 5 years
    Let's say you download a module as zip from Github. What are the steps you need to take to make it work? Can you just unzip it to the script folder and import it?