I'm a new Pycharm's user, and in my project, I need to turn a python file in a directory. This file contains a lot of functions and so i would like reorder my project creating a new directory that contains new python files with the functions cited previously. So my question is:
Is there a pycharm functionality that update automatically all references (for example import) in project?
I have searched something like this but i haven't found a good solution.
Thanks in advance
Regards
go to File >> Settings >> Project >> Project Interpreter
select the setting icon there and click on more
here click in last option (Interpreter Paths).
add your modules directory location here.
restart Pycharm
Related
Have been putting together applications that include their own infrastructure/deployment code using IntelliJ as my IDE. The total project as checked into Git includes several independent python scripts contained in sub directories under my project (including their package information and supporting python files). My IAC/deployment code takes care of making sure these scripts have a virtualenv with desired packages when on the infrastructure it is deployed to. All of this is working fine, except for when I try to get IntelliJ to understand the python subdirectory environments.
When I open the whole project tree as an IntelliJ project, there seems to be no way to explain to IntelliJ that some of the sub directories should be viewed as their own python project, such that they can have have their own virtualenv, packages, and base import directory understood. Being that IntelliJ doesn't understand these things, it sees my imports in these scripts as broken and I can't jump around in code etc.
As a work-a-round I have been sometimes just opening the python sub directories as their own IntelliJ project so that IntelliJ can understand them. But this is not ideal, as I have to have several different IntelliJ instances for the same larger project.
So now perhaps my question makes more sense, and I will restate it. Is there some way I am missing where I could have one instance of IntelliJ correctly understand the whole project including that some subdirectories are like python sub-projects with their own virtual env and packages etc?
A PyCharm/IntelliJ "project" can have "sub-projects". You access this feature by "attaching" one project to another. Open a project. Then open a second project. You'll get a dialog asking how you want to open the project, and one option will be "Attach". If you select this option, the second project is added to the already open window and your window now contains two somewhat independent projects.
If you now bring up the Preferences panel, and select "Project: xxx -> Project Interpreter", you should see that there are two projects listed in the central section of the panel. You can select each of these and configure the interpreter/environment settings for each project independently. You can also set up the Project Structure and Project Dependencies for each sub-project independently in this same way. This, I believe, is what you're asking how to do.
To illustrate, here's a screenshot showing just what this looks like, here with three projects being managed in a single window/main project:
Per the "Project: if-lab" section heading in the above screenshot, PyCharm/IntelliJ seems to have a notion that one of the projects is the primary project. You can see this in the Preferences pane, where the per-project settings are listed under a heading like "Project: xxx", where "xxx" is the name of the primary project.
I'm not sure just how the notion of one project being the primary comes into play.
Here is a page from the PyCharm documentation that explains some of this:
https://www.jetbrains.com/help/pycharm/opening-multiple-projects.html
The following section of that page explains how sub-projects interact with the primary project:
A newly opened project shares the same window as the already opened
one. The project that has already been opened, is considered the
primary project, and is always shown first in the Project tool window.
All the other projects are added to the primary project.
You can import classes and methods from dependent projects. Use the
Dependencies project settings to configure this behavior.
Some settings (such as Django, Buildout, Google App Engine, template
languages, Python interpreters, content roots) can be configured
separately for each project.
You can also configure different execution environments independent of the project's environment by creating multiple Run/Debug Configurations. These settings control what happens at execution time, but do not affect such things as syntax checking and highlighting, code completion, etc.
I've already accepted the very detailed answer by #Steve, and found it very useful to get me on the right track. But for my specific IntelliJ version(2020.1.2 on MacOS) I had to set it up a bit differently, so am including what I did as an alternate answer:
Note, that for my version of IntelliJ and this approach, the subdirectory to serve as a python root does NOT need to be a project itself, as is the case in Steve's answer.
Step 1) Set up a virtualenv for the subdirectory of your project which is to serve as the root your python code. I used python poetry, whatever method you use, note the path of the virualenv's python executable as you will need to enter it in IntelliJ later.
Step 2) Open File -> Project Structure. A window pops up (eventually took 30 seconds or so for me).
Step 3) On the panel at the left of the Project Structure window, select "Modules"
Step 4) A the top of the second column from the left is a + - and copy icon. Click the + icon. A drop down appears and you should select "Import Module", causing another pop up to appear.
Step 5) In the Pop Up, navigate to the subdirectory of your project that is to serve as the root directory of your python script and select it with the Open button at bottom right. The pop up window disappears and after a few seconds an "Import Module" popup window appears.
Step 6) In the Import Module window select "Create module from existing sources" then click the Next button and follow the Wizard like steps, which will give you the chance to specify the virtualenv path you setup in step 1.
Step 7) Click Finish for the Import Module window, and you should be set up.
Such as:
It can not show tipMember.objects when I type Member.o.(Member is a django model)
And same with python build-in function, like for,filter, map and so on.
This feature worked yesterday, I don't know why it broken now.
I have tried Invalidate Caches / Restart, do not work.
UPDATE
Maybe I know what is the problem.
I have a big folder name ALL project, it is my project. and project A under ALL project.
I accidentally mark project A excluded. And I use File -> Open .. to add it back.
It seems pycharm create .idea folder under project A, it means pycharm treat project A as a project.I have to set Project Interpreter for project A in order to get auto-completion back.
I don't want pycharm treat project A as a project. I want it treat project A as a folder, then it would use the Project Interpreter of ALL project.
Ok, I find that Project Structure dialog in File | Settings | Project Structure . Adding the folder back make every thing fine.
Have you set project interpreter correctly, i also had this problem when i started using pycharm, it can be also because of your editor settings file>editor>code-completion.
If I'm closing a project and then just delete the project folder, after PyCharm restarts an empty project folder is created again.
Just follow these steps in order. They assume you currently have the project open in a PyCharm window:
Close your project by clicking on File -> Close Project
Locate your project in the PyCharm file directory
Delete your project's directory
I agree that PyCharm's handling of what should be a very simple procedure is crappy. Maybe this will be improved in the future?
If you want to remove the project from the recent projects list, just highlight the project with your mouse and hit the del key.
This is for PyCharm 2019.1, but I can bet this will work in previous versions.
I am assuming you are in a Unix based operating system (macOs or any Linux distribution). I am also assuming you have currently opened the project you want to remove.
Close the project by going to File → Close Project:
Remove the project from Pycharm's Welcome Window:
Locate your project and delete it, you can use several alternatives, but I encourage you to use the following
$ rm -rf /path/to/your/project/directory
The previous command will work in macOS and Linux. In Windows, you can delete it manually by locating the projects directory using File Explorer.
That's it, the project has been completely deleted!
click the project you want to remove.
menu bar Edit-> Remove from Project View.
Version 2017.2.1
Press Alt+1 to bring the project pane, and make sure you're in
Project view (not Project Files view).
Select the desired project, and choose "Delete from project
view" from the context menu (right click). You can also use Delete
key.
There is a difference between PyCharm projects and the list of recently opened projects (File → Open recent...):
The PyCharm project is simply an arbitrary folder1 with the appropriate .idea subfolder in it.
The list of recently opened projects is simply the list of paths to some of such folders.
PyCharm does not keep the list of all projects, so you may safely delete a project folder (or its .idea subfolder) from your file system.
But if you have a bad luck, the path to your project directory is already listed in the list of recently opened projects, and if you try opening it with File → Open recent..., then
in older version of PyCharm, the deleted folder is re-created (as a new folder with only the .idea subfolder in it),
in newer versions of PyCharm you get the prompt similar as
The path C:\Users\you\PycharmProjects\deleted_project does not exist.
If it is on a removable or network drive, please make sure that the drive is
connected.
with buttons "OK" and "Remove From List", so you may simply remove the deleted folder from the list,
in all version of PyCharm, if you deleted only the .idea subfolder, it is silently re-created (but all your project settings are lost, of course).
To prevent from the uncomfortable consequence of the 1st possibility of using the File → Open recent... to re-create already deleted project, follow e.g. the first two instructions in the Simin Jie's answer.
1 generally a subfolder of the PyCharmProjects folder in your user profile directory (e.g. if you created it with File → New Project... and accepted the offered default path).
Works for Windows and MacOS.
File -- Open Recent -- Manage Projects
Cliek X to delete from Recent Projects
Delete local directory
According to https://www.youtube.com/watch?v=ksbmc0coO4k
In PyCharm 2018 I finally had to resort to deleting .idea folder and the project went away.
Highlight the project with your mouse and hit the del key, which works for me.
In 2021 maybe another way to do it. My problem was in project with user directory (root was /Users) that contains too many not Python files, and it triggers some problems with Git and file index.
What I did:
show hidden files (Cmd + Shift + .),
find .idea directories,
delete all .idea directories.
open PyCharm and add project from correct directory.
That's it.
I had the same issue. My solution was:
Close project.
Rename the folder you want to dissapear
Open pycharm project. A dialog will be shown asking if you want Pycharm remove the folder. Answer: yes.
That is enough.
In version 2017.2.1
File → Close Project
You will be taken to the start-up window, where you can see the projects you've created on the left. You can then delete a project by clicking the cross sign ('x') following the project name.
Relatively new to Python and PyCharm and as such most of the work. I'm doing is single files and therefore is not organized into projects.
Is there a way to configure Pycharm to be used to open single files and not use projects?
Similarly, is it possible to just run a single page/file?
On Pycharm 5.0.4...
Open the file (i.e. by double clicking in explorer)
Navigate to File->Settings...
Expand the Project tab on the left and select 'project interpreter' from the options that appear beneath
Set your project interpreter from the dropdown menu, and you should be able now to run your file without creating a project
You can always open a single file (File > Open) but to actually run or debug the code from within Pycharm, or use most of its other features, it needs a correctly configured project.
What you can do is create a single project to hold all of your "assorted" code. I usually recommend creating a virtualenv for this project as well and using that as your Pycharm project interpreter. You can further divide this project into different packages to keep things organized.
Update: PyCharm 4.5 now provide Scratch Files which seem relevant to this question.
You have to work with a configured project (indicated by folders) but there is a work around... In Pycharm when you create a project you will have a default file in .py or any other format.
Just follow the steps to work with individual files..
right click on the folder (in left hand side project tree) and add new python file
double click on the newly added file ,it will now be open in a new tab
(BUT..if you try to run this new file pycharm will still compile the previous file ...so to change that..)
right click on the tab of your file and click run "file name" or you can press CTRL+shift+F10
done :)
From the command line
pycharm [path_to_your_file]
where pycharm on my machine is aliased to /opt/pycharm-community-3.1.1/bin/pycharm.sh in file ~/.bash_aliases.
As Preston mentioned, not all features seem to work: for example, navigation between files does not seem to work.
You can also create a .py file and implement it within a PyCharm project by dragging it into the editor. This method also allows you to use a separate text editor to create your .py file.
I have a local git repository which has some Python modules and I would like to create a new project which includes these files (I installed egit egit).
My repository does not include a .project file.
It happened to me not so long ago.
I could not find a nice way to solve it, but found a simple way though :).
What I did was simple :
Create a brand new project, and let it empty.
Get the .project file wihch was created together with the project and add it in the git repository.
Now you can directly import your repo as a project.
Quite straightforward and simple.
I was also confused by this, and found the User Guide unhelpful.
I found the answer here: http://www.eclipse.org/forums/index.php/m/757705/
It is quite simple: you have to change the location in the "New Java Project" wizard, from the default location in your workspace to the folder in the cloned repository (probably ~/git/name_of_repo).
File > New PyDev Project
Unclick "Use default" and browse to your git repo.
Enter project name. Name must be the same as the git repo. You'll get a "Source Folder Not Found" error if it's not.
Click Finish.