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?
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.
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.
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.
A module is a file containing Python code. A package, however, is like a directory that holds sub-packages and modules.
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.
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.
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 variablePATH
).- 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With