Pycharm New Project
In PyCharm, a project is the most basic unit of work, containing source code, configurations, dependencies, and environment settings.
This section details how to create and manage Python projects in PyCharm.
* * *
## 1. Creating a New Project
### 1.1 Creating from the Welcome Screen
PyCharm provides many shortcuts upon startup:
!(#)
The interface upon first startup is as follows:
!(#)
Click "New Project".
You can also click "File" on the menu bar and then click "New Project".
!(#)
Configure project settings:
**Location:** Select and set the project storage path and name.
**Interpreter:** Project venv (recommended).
!(#)
βvenvβ is a module in Python used to create and manage virtual environments. A virtual environment is an isolated space that allows you to install and manage Python packages within it without affecting other Python environments on the system.
venv is part of the standard library for Python 3.3 and above, so no additional tools need to be installed to use itβ.
* Project venv: The virtual environment is stored by default in the venv folder under the project directory.
* Base Conda (if Anaconda/Miniconda is installed)
* Custom Environment
**Project Template:**
* Select the project type, such as "Pure Python" or "Flask" (for web development).
* A main.py file is created by default, containing a simple Python code template (e.g., print("Hello, World!")).
### 1.2 Creating a Project from Existing Code
If you already have Python code, you can import it in the following ways:
**Welcome Screen β "Open"**, select the project directory or **File β Open**, select the project folder.
PyCharm will automatically detect the Python environment; if not, you can manually configure the interpreter.
!(#)
* * *
## 2. Project Management
### 2.1 Project Structure
The default project structure in PyCharm is as follows:
my_project/ βββ .idea/ # PyCharm configuration files (e.g., run configurations, version control settings) βββ venv/ # Virtual environment (if using Virtualenv) βββ main.py # Example code (if "Create main.py" was checked) βββ Other files/directories # User-created code files
β οΈ Note: .idea/ and venv/ usually do not require manual modification, PyCharm manages them automatically.
### 2.2 Configuring the Python Interpreter
If the project needs to change the Python environment (e.g., switching from Python 3.8 to 3.10):
File β Settings β Project β Python Interpreter (Windows/Linux: Ctrl+Alt+S, macOS: β,).
Click βοΈ β Add Interpreter, select:
* **Virtualenv** (recommended)
* **System Python** (use the global Python directly)
* **Conda Environment** (if using Anaconda)
Select the Python version, click "OK".
### 2.3 Run/Debug Configurations
PyCharm allows setting different run methods for different scripts:
1. **Top toolbar β Run Configurations** β **"Edit Configurations"**.
2. Click **+**, select **Python**, configure:
* **Script path**: Select the `.py` file to run.
* **Python interpreter**: Ensure the correct environment is selected.
* **Parameters**: Command line parameters can be entered (e.g., `--port 8000`).
3. Click **"Apply"**, then you can click **βΆοΈ Run** or **π Debug**.
* * *
## 3. Common Project Operations
### 3.1 Opening Multiple Projects
PyCharm defaults to single-project mode (only one project can be open at a time), but multiple projects can be managed in the following ways:
1. **File β New β Project**, create a new project (PyCharm will prompt whether to open it in a new window).
2. Or **File β Open**, select another project directory.
### 3.2 Closing/Reopening a Project
* **Close current project**: `File β Close Project`, returns to the welcome screen.
* **Reopen recent project**: The welcome screen displays **"Recent Projects"**, click to reopen.
### 3.3 Project Dependency Management (requirements.txt / pyproject.toml)
PyCharm can automatically recognize `requirements.txt` or `pyproject.toml` and install dependencies:
Right-click requirements.txt β "Sync Python Requirements".
Or run in the Terminal:
pip install -r requirements.txt
To export the dependencies of the current environment:
pip freeze > requirements.txt
* * *
## 4. Frequently Asked Questions
**Q1: PyCharm cannot recognize Python files?**
* **Check file extension**: Ensure it is a `.py` file.
* **Check interpreter configuration**: `File β Settings β Python Interpreter`, ensure it is set correctly.
**Q2: How to delete a project?**
PyCharm does not automatically delete project files, you need to do it manually:
1. **Close the project** (`File β Close Project`).
2. **Delete the project folder in the file manager (e.g., Windows Explorer)**.
**Q3: How to backup/share a project?**
* **Recommended method**: Use Git (PyCharm has built-in Git support).
* **Manual method**: Copy the project folder (but it is recommended to exclude `.idea/` and `venv/`).
YouTip