Importing an external project into PyCharm

We have seen how the management of project types, virtual environments, and interpreters is done in PyCharm. In this final section, we will discuss the process of importing an existing project, previously external to PyCharm, into our workspace. This option is considerably useful, especially when you are working on collaborative projects that are cross-platform and cross-system. For example, you can import the source code of this book into your own PyCharm so that you don't have to manually enter the code yourself while following the examples in later chapters.

We will take a hands-on approach and try to import a small project that I previously prepared that's included in the GitHub repository for this book. Specifically, if you have already downloaded and unzipped the repository on your computer, the project we are trying to import into PyCharm is included in Chapter03/TestImport. To do this, we need to follow these steps:

  1. Go to File | Open... (or choose Open in the welcome window), navigate to that folder, and choose Open.
  2. A PyCharm project window will open. Inside, you will see the content of this project—a starting web development project with the Flask framework. The following is a screenshot of this project window, showing the main.py file in the editor:

Importing an external project into PyCharm
  1. In the preceding screenshot, you may have noticed—and potentially in your window as well—that there is a warning message above our editor that says No Python interpreter configured for the project. This should be expected since as we are importing an external project that has never been read by our PyCharm software before, so it won't know the specifics regarding the interpreter (and virtual environment) of the project.

Looping back to what we discussed previously, when a project is being worked on cross-platform, it is good practice to maintain separate virtual environments and interpreters in the different platforms. In other words, since different platforms may require different combinations of interpreters and libraries or packages, developers should only exchange and collaborate on the actual source code of the project, not including interpreters, virtual environments, or library dependencies.

If we look at the Python Console of the TestImport project we just imported into PyCharm, we will see that the global Python interpreter is being used, which supports the fact that PyCharm does not know which interpreter is appropriate for this specific external project.

  1. To select an interpreter, simply click on the text Configure Python interpreter (or open the settings and go to Project Interpreter). You will be taken to the interpreter-selecting window that we examined earlier. In my case, I will select my global Python interpreter for simplicity's sake.
  1. After choosing an interpreter for the project, another warning will potentially pop up, saying that the package and library requirements for this project are not being met:

Unsatisfied package requirements in imported projects
  1. Specifically, the tqdm module (that I arbitrarily chose to use as an example), which is being used in main.py and specified in requirements.txt, is not available for the Python interpreter I selected. If you don't have the tqdm module installed on your system, you will receive this warning as well. If you run the script, the execution will fail inside the try block in the code, and you will receive the following output:
You have successfully imported this project.
You do not have tqdm.
  1. Going back to the warning message, you can select the Install requirement option and PyCharm will automatically download and install all the unsatisfied packages for you.
  2. After the installation process has completed, rerunning the Python script again will give us a different output:
You have successfully imported this project.
You also have tqdm!

The reason PyCharm was able to detect the unsatisfied package requirement for tqdm is not because it analyzed the line of code where we import the package, but because we have the requirements.txt file in our project, which specifies that the dependency for this project is tqdm version 4.31.1.

As you already know, having a requirements.txt file that lists all the external libraries and packages a project requires is good practice in Python development that you should always follow, especially when working cross-platform. Our experiment just now has shown us that when we import a Python project with an appropriate requirements.txt file, PyCharm will assist us in the process of downloading and importing all the unmet dependency requirements.

For your reference, a quick command to generate the appropriate requirements.txt file for your project is pip freeze > requirements.txt, which should be run in your Terminal.
The pip freeze command lists all the external libraries and packages that your Python interpreter has, and the second part of the command will transfer the output data into a requirements.txt file.

Every time you'd like to transfer a project outside of your system and import it into PyCharm in another system, run this command and include requirements.txt in your project for a smooth transition, which will be assisted by PyCharm.

Notice that the combined usage of virtual environments and the requirements.txt file is common with Virtualenv. If the project you are importing uses Pipenv and Pipfile files as the virtual environment manager, PyCharm also offers the same functionalities to help you import your dependencies with one click.

Overall, we have learned how to import external projects into PyCharm. Moving forward with this book, you can go through this process again to import code examples from this book's GitHub repository into your own PyCharm while following the discussions in this book.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.69.255