Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding selenium webdriver as a python project dependence

Is there a way to install selenium webdriver as a python project dependence.

I need this in such a way, so when this project goes to a os, that doesn't have selenium webdriver installed not to be an issue for this project to run properly on that os.

Thank you in advance.

PS: Please take a look at my own answer to this question.

Stefan

like image 340
stefan.stt Avatar asked Sep 11 '26 03:09

stefan.stt


2 Answers

This is how I have done for all my projects.

  • Create a text file which has all the project dependencies mentioned. make sure you mentioned version as well. Example: requirement.txt

    • pytest==2.9.1
    • selenium==2.35.1
  • Create a Shell script or batch file which, creates a new virtual environment, installs all the dependencies and run the tests.
like image 121
Gaurang Shah Avatar answered Sep 13 '26 17:09

Gaurang Shah


I solved my problem this way:

  1. Installing pip for the current OS I am working on
  2. Installing selenium with the following command:

    pip install --install-option="--prefix=<path_to_dependencies_in_my_python_project>" -U selenium
    
  3. Import selenium module from this dependencies project folder

Also another approach is to check if the new OS has selenium installed and if not - install it via python script. The code looks something like that:

 # windows
 try:
     from selenium import webdriver
 except ModuleNotFoundError:
     os.system("pip install --install-option=\"--prefix={}\" selenium"
               .format(os.path.join(project_main_folder_path, "_depedencies")))
     from selenium import webdriver

# Linux
try:
    from selenium import webdriver
except:
    os.system("pip install selenium -t {}"
              .format(os.path.join(project_main_folder_path, "_depedencies", "Lib", "site-packages")))
    from selenium import webdriver

Take a look at the difference of the pathing - on Linux it is installed directly in the folder, while on windows it is installed in sub-folder "Lib\site-packages". Also I don't know why, but I had problems with name 'ModuleNotFoundError' is not defined.

like image 32
stefan.stt Avatar answered Sep 13 '26 17:09

stefan.stt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!