CCCC
CCCC

Reputation: 251

How can installed packages of a Python project in Pycharm IDE be used in every new Projects

Hello from the other side,

Python file on pycharm

I pip installed some packages using pycharm terminal: mysql connector, pymysql, pillow, etc needed for my programmes.

I have created some Python files and imported mysql connector, pymysql, PIL, etc and my codes worked fine.

Recently I tried creating a new python file in a new project, when I tried to import mysql connector and pymysql, my codes get red underline, notifying me that the imported modules can't be found.

These are the same packages I imported in my previous files and they worked fine.

So why are they turning red underline and greyed out now?

Do I need to be pip installing these packages for every new project?

I tried to see how I could resolve this by making some researches and asking some friends. I was advised to go to the settings and try to create a new environment with the installed packages of my choice, and then checking the boxes for global inheritance and making it available for other projects.

After this, the OK button was inactive (greyed), so I couldnt apply this change. I discovered I was notified that the enviroment rowbox wasnt empty and that I should use a new virtual environment which is empty but I really don't know how to create this.

Please if you've encountered something like this before or can assist, kindly give me a step-by-step approach.

Upvotes: 0

Views: 1689

Answers (2)

CCCC
CCCC

Reputation: 251

I really thank everyone who made effort to attempt my question. After much trial, I was able to resolve it and I will post how I resolved it step-by-step to enable others like me understand better.

First, when installing our Pycharm, a folder is automatically created for storing every project we shall be working on, this folder becomes the database of our projects. In most cases, folder is stored in our C:\drive.

Now, open your pycharm window (I mean your working template), File ----> open (this diplays the folders in your drive) ----> select the folder for storing your Pycharm projects and click ok. NOTE: do not open this folder to show your projects, just click on it and click ok. There are some situations whereby the Pycharm projects folder is already displayed on our pycharm window, this is because we already chose it during our installation. In a case like this, no need trying to bring it to the Projects displayed in our window as it'd already be displayed in the Projects in our window.

Your Pycharm projects folder will then display in your pycharm window under Projects.

Click on the Pycharm projects folder under your Projects and it will be highlighted blue.

Go to settings ----> Project: (your pycharm projects folder will also show here) By the right, select Project interpreter.

Do your normal installation of packages by clicking on the + sign at the right extreme (that is if you haven't installed any package before)

Then configure your environment setting by making sure the boxes for inheriting global site packages and making this available to other projects are checked.

When configuring your environment setting for the pycharm projects folder, the location path should also end with your pycharm projects folder\venv ; please do not include the name of any particular project in the location path as doing this will make these changes effective in only that particular project files.

Then click ok.

Whatever project your create under the Pycharm projects folder will then inherit every packages you've installed in the pycharm project settings.

I believe this will help those who have been having difficulty resolving this.

Upvotes: 0

Tanja Bayer
Tanja Bayer

Reputation: 811

Open File -> Settings

Select Project Interpreter than go to the gear wheel in the upper right corner and select add.

Use Virtual Environment, select your prefered base Interpreter in my case 3.6 and add the name for the virtual environment and where you want to store it

If you don't need to I would suggest to not inherit global site packages But you should select make it available to other projects.

Confirm it. Project Interpreter

Afterwards the virtual env should be selected in project interpreter if not select it from the list. Right beside the List you see a plus, you can use this to add new packages with python (doesn't work all the time for me -> then use terminal)

Selected Interpreter

Upvotes: 3

Related Questions