|
| 1 | +% |
| 2 | +\hsection{Virtual Environments in PyCharm}% |
| 3 | +% |
| 4 | +\begin{figure}% |
| 5 | +\centering% |
| 6 | +% |
| 7 | +\subfloat[][% |
| 8 | +We create a new project in \pycharm\ and select a \pgls{virtualEnvironment} as \menu{Custom Environment}. % |
| 9 | +We click~\menu{Create}.% |
| 10 | +\label{fig:venvPycharm1}% |
| 11 | +]{\tightbox{\includegraphics[width=0.48\linewidth]{\currentDir/venvPycharm1}}}% |
| 12 | +% |
| 13 | +\strut\hfill\strut% |
| 14 | +% |
| 15 | +\subfloat[][% |
| 16 | +Since I selected the folder where the other files (\textil{requirements.txt}, \textil{numpy_user.py}, \dots) are already located, \pycharm\ asks whether this is OK. % |
| 17 | +We select \menu{Create from Existing Sources} if asked.% |
| 18 | +\label{fig:venvPycharm2}% |
| 19 | +]{\tightbox{\includegraphics[width=0.48\linewidth]{\currentDir/venvPycharm2}}}% |
| 20 | +% |
| 21 | +\\[10pt]% |
| 22 | +% |
| 23 | +\subfloat[][% |
| 24 | +Let's take a look at \textil{requirements.txt}. % |
| 25 | +We double-click on this file.% |
| 26 | +\label{fig:venvPycharm3}% |
| 27 | +]{\tightbox{\includegraphics[width=0.48\linewidth]{\currentDir/venvPycharm3}}}% |
| 28 | +% |
| 29 | +\strut\hfill\strut% |
| 30 | +% |
| 31 | +\subfloat[][% |
| 32 | +It contains the one line \textil{numpy==1.26.4}, which means that our project requires \numpy\ in version~1.26.4.% |
| 33 | +\label{fig:venvPycharm4}% |
| 34 | +]{\tightbox{\includegraphics[width=0.48\linewidth]{\currentDir/venvPycharm4}}}% |
| 35 | +% |
| 36 | +% |
| 37 | +\caption{Using \textil{requirements.txt} and \pglspl{virtualEnvironment} in \pycharm~(part~1).}% |
| 38 | +\label{fig:venvPycharmA}% |
| 39 | +\end{figure}% |
| 40 | +% |
| 41 | +\begin{figure}% |
| 42 | +\ContinuedFloat% |
| 43 | +\centering% |
| 44 | +% |
| 45 | +\subfloat[][% |
| 46 | +We now click on \textil{numpy_user.py} and get informed that required \pglspl{package} are missing. % |
| 47 | +We can install them into our project's \pgls{virtualEnvironment} by clicking \menu{Install requirement}. % |
| 48 | +Notice:~This question could also have popped up when we opened \textil{requirements.txt}, in which case we would have installed the requirements then.% |
| 49 | +\label{fig:venvPycharm5}% |
| 50 | +]{\tightbox{\includegraphics[width=0.48\linewidth]{\currentDir/venvPycharm5}}}% |
| 51 | +% |
| 52 | +\strut\hfill\strut% |
| 53 | +% |
| 54 | +\subfloat[][% |
| 55 | +We get informed that the required packages were successfully installed.% |
| 56 | +\label{fig:venvPycharm6}% |
| 57 | +]{\tightbox{\includegraphics[width=0.48\linewidth]{\currentDir/venvPycharm6}}}% |
| 58 | +% |
| 59 | +\\[10pt]% |
| 60 | +% |
| 61 | +\subfloat[][% |
| 62 | +We right-click on \textil{numpy_user.py} and click on~\menu{Run \inSQuotes{numpy\_user.py}}.% |
| 63 | +\label{fig:venvPycharm7}% |
| 64 | +]{\tightbox{\includegraphics[width=0.48\linewidth]{\currentDir/venvPycharm7}}}% |
| 65 | +% |
| 66 | +\strut\hfill\strut% |
| 67 | +% |
| 68 | +\subfloat[][% |
| 69 | +Our program is executed without error and produces the expected output.% |
| 70 | +\label{fig:venvPycharm8}% |
| 71 | +]{\tightbox{\includegraphics[width=0.48\linewidth]{\currentDir/venvPycharm8}}}% |
| 72 | +% |
| 73 | +% |
| 74 | +\caption{Using \textil{requirements.txt} and \pglspl{virtualEnvironment} in \pycharm~(part~2).}% |
| 75 | +\label{fig:venvPycharmB}% |
| 76 | +\end{figure}% |
| 77 | +% |
| 78 | +The \pgls{IDE} \pycharm\ also supports using \pglspl{virtualEnvironment} and \textil{requirements.txt} files. |
| 79 | +Let us here demonstrate this on the example program that requires \numpy\ which was given as \cref{lst:packages:numpy_user} and \textil{requirements.txt} file given in \cref{lst:packages:requirementstxt}. |
| 80 | +In \cref{fig:venvPycharmA}, we work through the steps to create and manage a project with a \pgls{virtualEnvironment} in \pycharm\ on their basis. |
| 81 | + |
| 82 | +First, we need to create a new project~(\cref{fig:venvPycharm1}). |
| 83 | +For this purpose, we already copied the files \pglspl{virtualEnvironment} and \textil{requirements.txt} into a folder~(here:~\textil{/tmp/packages}). |
| 84 | +In the \menu{New Project} dialog of \pycharm, we select this folder as \menu{Location:}. |
| 85 | +As \menu{Interpreter type:}, we choose \menu{Custom Environment} and as \menu{Environment:}, we pick \menu{Generate new}. |
| 86 | +As \menu{Type:}, we choose \menu{Virtualenv}. |
| 87 | +The default \menu{Location} is \textil{.venv} inside our project directory and we keep this setting. |
| 88 | +After clicking \menu{Create}, a new project is created. |
| 89 | + |
| 90 | +Well, almost: |
| 91 | +\Cref{fig:venvPycharm2} reminds me that I copied some files into the project folder before creating the project. |
| 92 | +\pycharm\ wants to make sure that this is right and asks me so. |
| 93 | +As answer, I click~\menu{Create from Existing Sources}. |
| 94 | + |
| 95 | +We now are in the normal \pycharm\ project view in~\cref{fig:venvPycharm3}. |
| 96 | +All the files that I placed into the folder are there and also a \textil{.venv} directory has been created. |
| 97 | +Notice that you could as well create an empty project and copy the files there. |
| 98 | + |
| 99 | +We click on \textil{requirements.txt} and it opens in~\cref{fig:venvPycharm4}. |
| 100 | +Indeed, the file prescribes a single requirement, \numpy\ in version~1.26.4. |
| 101 | + |
| 102 | +When we click on the file \textil{numpy_user.py} to open it in~\cref{fig:venvPycharm5}, we notice a yellow bar at the top of the file's contents. |
| 103 | +This bar tells us that the required package \numpy\ is missing. |
| 104 | +It offers us the two choices to either \menu{Install requirement} or to \menu{Ignore requirement}. |
| 105 | +We select the first option and click it. |
| 106 | +Notice that this same yellow bar could also have appeared when we opened \textil{requirements.txt}. |
| 107 | +I am not sure why it appears only now. |
| 108 | +Either way, we accept the choice to install the requirement. |
| 109 | + |
| 110 | +In \cref{fig:venvPycharm6}, we are informed that the installation was successful. |
| 111 | +The small overlay also tells us that \numpy\ was installed in version~1.26.4, as prescribed by the \textil{requirements.txt} file. |
| 112 | +At the time of this writing, \numpy\ is already out at version~2.2.1, which would have been used if we had just installed it without version specification. |
| 113 | +So this confirms that, indeed, our \textil{requirements.txt} is used. |
| 114 | +We also can see that the red underline under \pythonilIdx{numpy} that was present in \cref{fig:venvPycharm5} is now gone in \cref{fig:venvPycharm6}, because now the package and corresponding modules can be loaded without error. |
| 115 | + |
| 116 | +As final check that everything went well we run our program \textil{numpy_user.py} in \cref{fig:venvPycharm7}. |
| 117 | +We right-click the file in three view on the left-hand side of the window. |
| 118 | +A popup menu appears, in which we click on \menu{Run \inSQuotes{numpy\_user.py}}. |
| 119 | +We could just as well have pressed \keys{\ctrl+\shift+F10}. |
| 120 | +Our program is executed and the expected output appears (\cref{fig:venvPycharm8}). |
| 121 | +All is well.% |
| 122 | +% |
| 123 | +\FloatBarrier% |
| 124 | +\endhsection% |
| 125 | +% |
0 commit comments