Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 934
PeepingHog Avatar asked Apr 06 '16 18:04

PeepingHog


People also ask

How do I use a Python module without installing it?

If you are not able to install modules on a machine(due to not having enough permissions), you could use either virtualenv or save the module files in another directory and use the following code to allow Python to search for modules in the given module: >>> import os, sys >>> file_path = 'AdditionalModules/' >>> sys.

Does importing a module run it?

When you import a module in Python, all the code in it will be run, and all the variables in that module will be stuck on that module object.

What happens when we import a module?

If there is an existing module object with the given name in sys.modules , import will have already returned it. The module will exist in sys.modules before the loader executes the module code.

Is a module the same as a package?

A module is a file containing Python code. A package, however, is like a directory that holds sub-packages and modules.

What are the two ways to import module?

The 4 ways to import a moduleImport the whole module using its original name: pycon import random. Import specific things from the module: pycon from random import choice, randint. Import the whole module and rename it, usually using a shorter variable name: pycon import pandas as pd.


2 Answers

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.

like image 64
Reti43 Avatar answered Sep 19 '22 19:09

Reti43


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
like image 20
Maxim Suslov Avatar answered Sep 18 '22 19:09

Maxim Suslov