Python environment configuration in Pycharm

1. Discovery of the problem

Recently, when installing and running jupyter notebook using the command-line tool under Pycharm, kernal has always reported errors. The bottom two lines of the error are as follows

    import win32api
ImportError: DLL load failed: 找不到指定的模块。

After a web search, it was found that the path of python could not be parsed correctly. It may be that the path of the python interpreter may conflict. Later, I found that there are "two" python paths, one is the path where I installed python before, and the other is in my project code A venv directory under the directory. I thought that pycharm installed another side of python in my directory, so I uninstalled it. But after uninstalling and then running the program, I get an error saying that I can't find python in the directory where I uninstalled. I'm surprised that you don't have a python in the project directory, how can you find me to install it again.

After some searching, I figured out that the python in the original project directory is a virtual environment. It is a virtual environment created by pycharm based on the system python. If the system python is deleted, it can't be used. And I have been using the virtual environment created by Pycharm before. I installed and started jupyter notebook in the virtual environment, which caused the conflict of the python interpreter in the virtual environment.

2. The interpreter in Pycharm

So the question is, when did Pycharm create the virtual environment?

After installing Pycharm and creating a new Python project, the following dialog box will pop up. If you do not click the Project Interpreter option, a virtual environment will be created by default. After clicking, you can see that there are three environmental management options in the New environment using option

The first Virtualenv is Pycharm's integrated environment management and management tool. It creates a virtual environment under the project folder Location according to the system's python interpreter Base interpreter, and has an independent library and interpreter interpreter, isolated from the external environment , So that the version that the files in the project depend on will not be affected by other library files. When a compatibility issue occurs after the global or other library file version is updated, it will not affect the project in Virtualenv. Checking Inherit global site-packgaes can inherit and use the library files from the globally installed Python. Checking Make available to all projects can enable the library files downloaded by this project to be called externally.

The second Pipenv is a dependency management tool for Python. Imagine that if you develop Python in one environment, you need to run or develop it in another environment, and the type and version of the dependency package in another environment cannot be the same as before. At this time, you need a tool to manage the dependencies and versions in the python project. pipenv project will be created under the current folder Pipfileand Pipfile.lockfile for record and manage project dependencies used when the need to deploy a new environment projects only need to download the corresponding package to the record pipfile in.

The third Conda is also a package environment management tool. The environment management tool for it is Anaconda or Miniconda. This application will manage the Python environment in the system. Through it, you can create multiple different development environments in the system. For example, the pytho version of one environment is 2.7, and the other is 3.7. When you need to use the 3.7 version, start the environment through anaconda, and the dependent packages installed and configured in the environment are invisible in the other environment. Pycharm can be introduced into the Anaconda environment that has been configured in the system and used directly. Note that when configuring the interpreter in Pycharm, you need to select the file location of python.exe. If you are creating an anaconda environment, the file is saved in the anaconda / envs folder. You need to find the interpreter python.exe in the corresponding environment.

You can also use the globally installed Python in the system , check Existing Interpreter and find the location of the global system installation Python.exe

3. Pycharm configure remote server

The Remote Host tool integrated with Pycharm can be easily connected to a remote server for viewing and code synchronization of server files. By clicking Tools-> Deployment-> Browse Remot Host in the menu bar, the following configuration dialog box pops up, enter the server related information to connect to the specified server, and the directory corresponding to the root path is displayed in the Remote Host sidebar on the right side of Pycharm.

If you want to achieve project synchronization, you can define the second tab of the dialog box, Mappings, to map the local project directory to the specified directory of the server

After mapping, right-click a file in the project, find Deployment in the pop-up option, you can upload to the server Uploaded to Server, or download the corresponding version from the server Download from Server

In addition, you can replace the remote compiler of Python. Click Pycharm settings as shown below, find Project Interpreter, then click the gear on the right side to add, and then select SSH Interpreter in Add Python Interpreter. You can create a new remote server, or you can choose just The remote server has been configured. Then find the location of python in the remote server in the pop-up window to complete the configuration of the remote compiler

4. Pycharm connects to the database

Idea integrated database tool is also very convenient, click View-> Tool Windows-> Database to open the database plugin in the right sidebar

Click the + sign in the sidebar to select Data Source and select the database source. I select the Mysql type. The following dialog box pops up, fill in the host name of the database Host, user name user, password password, you can choose a certain to connect to the database A specific database Database, and then click OK to connect to the database of the specified host. During the first link, you will be prompted that the database connection plug-in is missing, and click Install.

The more convenient place is that after connecting to a specified library, when you write a SQL statement in a python program, language detection will be performed. Press alt + shift + enter to set the database of the current project to Mysql, and the code will automatically prompt for supplement Keywords, table names, and field names in the entire database

Published 124 original articles · Like 65 · Visit 130,000+

Guess you like

Origin blog.csdn.net/theVicTory/article/details/103037268