安装指南:Python、PyCharm、Selenium 和 Web 驱动程序
本指南旨在简化 Python、PyCharm 和 Selenium 的安装过程。我创建此草稿是为了帮助缓解许多人在搜索全面的安装说明时遇到的困难。无论是练习还是工作,遵循这些步骤都将确保顺利的设置体验。
第1步:安装Python
视窗
-
下载Python:
- 前往Python官方网站。
- 下载最新版本的Python。
-
运行安装程序:
- 运行下载的安装程序。
- 选中“将 Python 添加到 PATH”复选框。
- 点击“立即安装”。
- 点击“禁用最大路径限制(255)”。
macOS
-
下载Python:
- 前往Python官方网站。
- 下载最新版本的Python。
-
运行安装程序:
- 打开下载的.pkg 文件。
- 按照安装说明进行操作。
当然!以下是 macOS 上 Python 的安装说明:
macOS 上 Python 的安装说明
第一步:下载Python
-
前往Python官方网站:
-
下载最新版本:
- 网站应自动检测您的操作系统并建议 macOS 的最新版本。
- 点击“下载Python [版本]”按钮下载安装程序。
第二步:安装Python
-
运行安装程序:
- 下载完成后,找到下载的文件,通常位于您的下载文件夹中。
- 双击 .pkg 文件打开 Python 安装程序。
-
按照安装步骤操作:
- 安装程序将打开一个新窗口。单击“继续”开始安装过程。
- 查看并同意许可协议。
- 选择安装位置。默认位置通常就可以。
- 点击“安装”开始安装。
- 系统可能会提示您输入 macOS 密码以授权安装。输入您的密码并单击“安装软件”。
-
完成安装:
第 3 步:验证安装
-
打开终端:
- 您可以在应用程序中找到终端>;实用程序或使用 Spotlight 搜索(Cmd + Space 并输入“Terminal”)。
-
检查Python版本:
-
检查 pip 版本:
步骤 4:设置环境变量(可选)
-
将 Python 添加到 PATH:
- 打开终端并输入以下命令以在文本编辑器中打开 .bash_profile 或 .zshrc 文件:
或
nano ~/.bash_profile
登录后复制
-
编辑文件:
export PATH="/Library/Frameworks/Python.framework/Versions/3.x/bin:$PATH"
登录后复制
- 按 Ctrl + O 保存文件,然后按 Enter。按 Ctrl + X 退出。
-
应用更改:
或
source ~/.bash_profile
登录后复制
第5步:安装Virtualenv(可选)
-
安装virtualenv:
- 要创建隔离的Python环境,您可以使用virtualenv。使用 pip 安装它:
pip3 install virtualenv
登录后复制
-
创建虚拟环境:
cd path/to/your/project
登录后复制
-
激活虚拟环境:
- Activate the virtual environment:
source venv/bin/activate
登录后复制
- You will see (venv) in your terminal prompt, indicating the virtual environment is active.
-
Deactivate the Virtual Environment:
- To deactivate the virtual environment, simply run:
You have now installed Python on your macOS system, set up your environment variables, and optionally installed virtualenv for managing your projects.
Linux
-
Using APT (Debian/Ubuntu):
sudo apt update
sudo apt install python3 python3-pip
登录后复制
-
Using DNF (Fedora):
sudo dnf install python3 python3-pip
登录后复制
Step 2: Install PyCharm
-
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
-
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
-
Download PyCharm:
- Go to the JetBrains PyCharm download page.
- Download the Community Edition (free) or Professional Edition (paid).
-
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".
-
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
-
Download PyCharm:
- Go to the JetBrains PyCharm download page.
- Download the Community Edition (free) or Professional Edition (paid).
-
Install PyCharm:
- Open the downloaded .dmg file.
- Drag and drop the PyCharm application into the Applications folder.
-
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
-
Download PyCharm:
- Go to the JetBrains PyCharm download page.
- Download the tar.gz package for the Community Edition (free) or Professional Edition (paid).
-
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
登录后复制
-
Create a Desktop Entry (optional):
- While running PyCharm, go to the main menu and select "Tools" > "Create Desktop Entry".
First-time Startup
-
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.
-
Customize PyCharm:
- Choose your UI theme (Light or Dark).
- Configure additional settings as needed.
-
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
-
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:
Check pip Installation:
Update pip Installation:
python -m pip install --upgrade pip
登录后复制
-
Verify if Selenium Installed Properly or Not:
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:
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.
-
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.
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()
登录后复制
-
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
-
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.
-
Open Advanced System Settings:
- In the System window, click on Advanced system settings on the left-hand side.
-
Open Environment Variables:
- In the System Properties window, click on the Environment Variables... button near the bottom.
Step 2: Edit the PATH Variable
-
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.
-
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.
-
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
-
Open Command Prompt:
- Press Win + R, type cmd, and press Enter to open the Command Prompt.
-
Check Python Version:
- Type the following command and press Enter:
- 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中文网其他相关文章!