首頁 > 後端開發 > Python教學 > Python、Pycharm、Selenium 和 Web 驅動程式的安裝

Python、Pycharm、Selenium 和 Web 驅動程式的安裝

WBOY
發布: 2024-07-18 12:34:48
原創
1028 人瀏覽過

Installation of Python, Pycharm, Selenium and Web Drivers

安裝指南:Python、PyCharm、Selenium 和 Web 驅動程式

本指南旨在簡化 Python、PyCharm 和 Selenium 的安裝過程。我創建此草稿是為了幫助緩解許多人在搜尋全面的安裝說明時遇到的困難。無論是練習還是工作,遵循這些步驟都將確保順利的設定體驗。

第1步:安裝Python

視窗

  1. 下載Python:

    • 前往Python官方網站。
    • 下載最新版本的Python。
  2. 執行安裝程式:

    • 執行下載的安裝程式。
    • 選取「將 Python 新增至 PATH」複選框。
    • 點選「立即安裝」。
    • 點選「停用最大路徑限制(255)」。

macOS

  1. 下載Python:

    • 前往Python官方網站。
    • 下載最新版本的Python。
  2. 執行安裝程式:

    • 開啟下載的.pkg 檔。
    • 依照安裝說明進行操作。 當然!以下是 macOS 上 Python 的安裝說明:

macOS 上 Python 的安裝說明

第一步:下載Python

  1. 前往Python官方網站:

    • 開啟網頁瀏覽器並導航至 Python 官方網站。
  2. 下載最新版本:

    • 網站應自動偵測您的作業系統並建議 macOS 的最新版本。
    • 點選「下載Python [版本]」按鈕下載安裝程式。

第二步:安裝Python

  1. 執行安裝程式:

    • 下載完成後,找到下載的文件,通常位於您的下載資料夾中。
    • 雙擊 .pkg 檔案開啟 Python 安裝程式。
  2. 依照安裝步驟操作:

    • 安裝程式將開啟一個新視窗。按一下“繼續”開始安裝程序。
    • 查看並同意授權協議。
    • 選擇安裝位置。預設位置通常就可以。
    • 點選「安裝」開始安裝。
    • 系統可能會提示您輸入 macOS 密碼以授權安裝。輸入您的密碼並點擊“安裝軟體”。
  3. 完成安裝:

    • 安裝完成後,按一下「關閉」退出安裝程式。

第 3 步:驗證安裝

  1. 開啟終端機:

    • 您可以在應用程式中找到終端機>;實用程式或使用 Spotlight 搜尋(Cmd + Space 並輸入「Terminal」)。
  2. 檢查Python版本:

    • 在終端機中,鍵入以下命令並按 Enter:
     python3 --version
    
    登入後複製
  • 您應該會看到您安裝的 Python 版本。例如:

     Python 3.x.x
    
    登入後複製
  1. 檢查 pip 版本:

    • 在終端機中,鍵入以下命令並按 Enter:
     pip3 --version
    
    登入後複製
  • 您應該會看到隨 Python 安裝的 pip 版本。例如:

     pip 21.x.x from /Library/Frameworks/Python.framework/Versions/3.x/lib/python3.x/site-packages/pip (python 3.x)
    
    登入後複製

步驟 4:設定環境變數(可選)

  1. 將 Python 加入 PATH:

    • 開啟終端機並輸入以下命令以在文字編輯器中開啟 .bash_profile 或 .zshrc 檔案:
     nano ~/.zshrc
    
    登入後複製


     nano ~/.bash_profile
    
    登入後複製
  2. 編輯檔案:

    • 將以下行加入文件:
     export PATH="/Library/Frameworks/Python.framework/Versions/3.x/bin:$PATH"
    
    登入後複製
  • 按 Ctrl + O 儲存文件,然後按 Enter。按 Ctrl + X 退出。
  1. 應用更改:

    • 在終端機中,執行以下命令來套用變更:
     source ~/.zshrc
    
    登入後複製


     source ~/.bash_profile
    
    登入後複製

步驟5:安裝Virtualenv(可選)

  1. 安裝virtualenv:

    • 要建立隔離的Python環境,您可以使用virtualenv。使用 pip 安裝它:
     pip3 install virtualenv
    
    登入後複製
  2. 建立虛擬環境:

    • 導航到您的專案目錄:
     cd path/to/your/project
    
    登入後複製
  • 建立虛擬環境:

     virtualenv venv
    
    登入後複製
  1. 啟動虛擬環境:

    • Activate the virtual environment:
     source venv/bin/activate
    
    登入後複製
  • You will see (venv) in your terminal prompt, indicating the virtual environment is active.
  1. Deactivate the Virtual Environment:

    • To deactivate the virtual environment, simply run:
     deactivate
    
    登入後複製

You have now installed Python on your macOS system, set up your environment variables, and optionally installed virtualenv for managing your projects.

Linux

  1. Using APT (Debian/Ubuntu):
   sudo apt update
   sudo apt install python3 python3-pip
登入後複製
  1. Using DNF (Fedora):
   sudo dnf install python3 python3-pip
登入後複製

Step 2: Install PyCharm

  1. Download PyCharm:

    • Go to the JetBrains PyCharm website.
    • Download the Community Edition (free) or Professional Edition (paid).
    • Always remember to Check the System version and download compatible version else, you will able to install but not able open the pycharm software
  2. Run the Installer:

    • Follow the installation instructions provided by JetBrains.
    • Certainly! Here are the installation instructions for PyCharm as provided by JetBrains:

Installation Instructions for PyCharm

Windows

  1. Download PyCharm:

    • Go to the JetBrains PyCharm download page.
    • Download the Community Edition (free) or Professional Edition (paid).
  2. Run the Installer:

    • Double-click the downloaded .exe file to launch the installer.
    • Follow the setup wizard:
      • Click "Next" to continue.
      • Choose the installation location and click "Next".
      • Select the desired installation options:
      • Create a desktop shortcut.
      • Add the “bin” folder to the PATH (recommended).
      • Associate .py files with PyCharm.
      • Add an open folder as a project option.
      • Click "Install".
    • Once the installation is complete, click "Finish".
  3. Launch PyCharm:

    • After the installation, you can start PyCharm by double-clicking the PyCharm shortcut on your desktop or by searching for PyCharm in the Start menu.

macOS

  1. Download PyCharm:

    • Go to the JetBrains PyCharm download page.
    • Download the Community Edition (free) or Professional Edition (paid).
  2. Install PyCharm:

    • Open the downloaded .dmg file.
    • Drag and drop the PyCharm application into the Applications folder.
  3. Launch PyCharm:

    • Open Finder and go to the Applications folder.
    • Find PyCharm and double-click to open it.
    • If you see a warning about opening an application downloaded from the internet, click "Open".

Linux

  1. Download PyCharm:

    • Go to the JetBrains PyCharm download page.
    • Download the tar.gz package for the Community Edition (free) or Professional Edition (paid).
  2. Install PyCharm:

    • Open your terminal.
    • Navigate to the directory where the tar.gz file was downloaded.
    • Extract the tar.gz file:
     tar -xzf pycharm-*.tar.gz
    
    登入後複製
  • Move to the extracted directory:

     cd pycharm-*/
    
    登入後複製
  • Run PyCharm:

     ./bin/pycharm.sh
    
    登入後複製
  1. Create a Desktop Entry (optional):
    • While running PyCharm, go to the main menu and select "Tools" > "Create Desktop Entry".

First-time Startup

  1. Activate PyCharm:

    • On the first startup, you will be prompted to activate PyCharm.
    • For the Community Edition, select "Evaluate for free" and click "Evaluate".
    • For the Professional Edition, enter your JetBrains account credentials or use a license key.
  2. Customize PyCharm:

    • Choose your UI theme (Light or Dark).
    • Configure additional settings as needed.
  3. Create or Open a Project:

    • You can now create a new project, open an existing project, or check out a project from version control.

For more detailed instructions and troubleshooting, refer to the official PyCharm installation guide.

Step 3: Install Selenium

  1. Using pip:

    • Open your command line or terminal, Run as Administrator
    • Install Selenium using the following command:
     pip install selenium
    
    登入後複製

Step 4: Verify Installation

Check Python Installation:

   python --version
登入後複製

Check pip Installation:

   pip --version
登入後複製

Update pip Installation:

python -m pip install --upgrade pip
登入後複製
  1. Verify if Selenium Installed Properly or Not:
pip show selenium
登入後複製

You should get the selenium details

Web Drivers:
For the latest Selenium Version: 4.22.0, Web Drivers have been pre installed
You can verify in path:

C:\Users\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver
登入後複製

you will see the wedrivers for IE, Chrome, Edge, FireFox, Remote, Safari, Chromium.

Verify Selenium Installation:

  • Open a Python interactive shell by typing python in your command line or terminal.
  • Run the following commands:

     import selenium
     print(selenium.__version__)
    
    登入後複製

Example: Basic Selenium Script

Here's a simple script to verify your Selenium setup. This script opens a web browser and navigates to a website.

  1. Install WebDriver:

    • Download the appropriate WebDriver for your browser (e.g., ChromeDriver for Chrome) from Selenium’s official site.
    • Make sure to place the WebDriver executable in a directory that is in your PATH.
  2. Create a Python Script:

   from selenium import webdriver

   # Initialize the WebDriver (assuming ChromeDriver is in your PATH)
   driver = webdriver.Chrome()

   # Open a website
   driver.get("https://www.python.org")

   # Print the title of the page
   print(driver.title)

   # Close the browser
   driver.quit()
登入後複製
  1. Run the Script:

    • Save the script as test_selenium.py.
    • Run the script in your command line or terminal:
     python test_selenium.py
    
    登入後複製

If everything is set up correctly, you should see the browser open, navigate to the Python website, and print the page title in the console.

Additional Selenium Web Drivers Configuration:

If you are using earlier versions or not able to see webdriver, you can download them or For more details you can see visit:
https://pypi.org/project/selenium/

After downloading the files, extract the files and copy the drivers.exe and place in the path or accordingly to your system files path

C:\Users\AppData\Local\Programs\Python\Python311\
登入後複製

This is a pre installed path for python that is available in the environment variables, which you pretty much can use without errors

or You can create a folder in "C drive" and add that file path in environment variables.

Driver "PATH" Configuration:
Here are the instructions on how to add the path C:\Users\AppData\Local\Programs\Python\Python311\ or any "PATH" to the environment variables in Windows:

Adding Python Path to Environment Variables in Windows

Step 1: Open Environment Variables Settings

  1. Open System Properties:

    • Press Win + Pause/Break to open the System window.
    • Alternatively, you can right-click on the This PC or Computer icon on your desktop or in File Explorer and select Properties.
  2. Open Advanced System Settings:

    • In the System window, click on Advanced system settings on the left-hand side.
  3. Open Environment Variables:

    • In the System Properties window, click on the Environment Variables... button near the bottom.

Step 2: Edit the PATH Variable

  1. Locate the PATH Variable:

    • In the Environment Variables window, you will see two sections: User variables and System variables.
    • Scroll down in the System variables section and find the variable named Path. Select it and click on the Edit... button.
  2. Add New Path:

    • In the Edit Environment Variable window, you will see a list of paths. Click on the New button to add a new path.
    • Enter the path C:\Users\AppData\Local\Programs\Python\Python311\ in the new entry.
  3. Save Changes:

    • After adding the path, click OK to close the Edit Environment Variable window.
    • Click OK again to close the Environment Variables window.
    • Finally, click OK to close the System Properties window.

Step 3: Verify the Path Addition

  1. Open Command Prompt:

    • Press Win + R, type cmd, and press Enter to open the Command Prompt.
  2. Check Python Version:

    • Type the following command and press Enter:
     python --version
    
    登入後複製
  • You should see the version of Python installed in C:\Users\AppData\Local\Programs\Python\Python311\

If everything is set up correctly, the command prompt should display the Python version, confirming that the path has been successfully added to the environment variables.

These steps ensure that Python can be accessed from any command prompt window without needing to specify the full path to the executable each time.


以上是Python、Pycharm、Selenium 和 Web 驅動程式的安裝的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板