Indexing of external library fails in PyCharm 4.5 - python

This issue has come up with previous versions of PyCharm (see this SO post and this one), but it manifests somewhat differently in 4.5.
I am trying to add another library to the Python path of my current project. This is an internal library and consists of a bunch of .py files in a different directory from the current project. I carry out the following steps:
Go to File -> Settings -> Project: summary -> Project Interpreter
Click on the gear icon near the top right corner of the screen that appears in the dialog
Select More... from the context menu that pops up
Click on the interpreter I am using
Click on the last of the 4 icons to the right of that, the thing with a folder and arrows, with tooltip "Show paths for the selected interpreter"
Push + in the resulting popup
Use the file dialog to add the path of the library
Having done this, the result is:
Good:
Running the current project code from a Run Configuration works; that is, the external library is picked up in the PYTHONPATH
The added path does not immediately disappear from the interpreter path dialog, as it was doing in 4.0 (see comment to this answer in one of the SO posts mentioned above).
Bad:
Indexing of the new library fails, and all references to the external library are marked as unresolved references in the editor
I have even tried File -> Invalidate Caches / Restart... and pressed the Invalidate and Restart button that appears. After sitting and waiting for indexing to finish, I get the same result. I have been very careful with setting the right path, and it seems to be correct, given that running the code actually works.
Does anyone know of a workaround for this issue, short of adding the external code as a content root?

I ran into a very similar issue. I am working on an OpenStack component, and all third-party libraries were getting marked as unresolved references. It turned out to be because the .tox directory is automatically excluded by PyCharm, which prevents any virtual environments in that directory from getting indexed properly.
To fix this, I went to the Editor > File Types dialog of the Preferences menu, and removed the .tox folder from the Ignore files and folders option.

Checked w/ JetBrains support, they confirmed that the only ways to add external libraries to a PyCharm project are:
Add the library as a Content Root
OR
Open it as separate project in the same window and attach it to your current project
Not the cleanest approaches, as they both basically mean adding the other library's actual code to your project. But they are the only ones at this point.

Related

Importing modules from other folders: how to display function arguments in Spyder or PyCharm?

There are loads of answers on how to import modules from other folders.
The answer always seems to be along the lines of:
import sys
sys.path.insert(0,"c://UserName//MyFolder//MyBeautifulCode")
import myscript as ms
after which you can run ms.my_fun(x,y,z) where my_fun() is defined in c://UserName//MyFolder//MyBeautifulCode//myscript.py
The code runs; however, what doesn't work is that, this way, I do not get the usual tooltip showing the arguments of my_fun(); if instead I copy myscript.py in the very same folder as the script I am currently running, then, yes, I do get the tooltip.
What I mean is I don't see something like this:
I have tried with both PyCharm and Spyder and the behaviour, in this respect, is the same for both.
I suppose this happens because c://UserName//MyFolder//MyBeautifulCode//myscript.py gets added to the path only when the script is run so, before it is run, the IDE doesn't find my_fun()
Is this correct?
If so, is the only solution to manually add c://UserName//MyFolder//MyBeautifulCode//myscript.py to the path ?
By the way, I am talking about a couple of functions which I reuse in 3 separate programs I am running. It is nothing worth publishing on github or pip as a package or anything like that.
For PyCharm, you need to set up your project's venv settings to include that path as well. It took me a lot of time to find it at first - and I used google to search for this! but apparently PyCharm hid the option deeper... well, see for yourself.
Go to the Settings, Project: [your project name here], Python Interpreter
See the cog on the right? Click it, "Show all". This will show up, listing all venvs that PyCharm can use for your project:
With your venv selected, click the last icon at the bottom. The icon looks kinda like a folder structure.
Now we see all the paths recognised by the selected interpreter in PyCharm. We can click + to add a new path. Manually added path will have "(added by user)` at the end, just like in the pic.
If you insert a path into the Python path in your code, it's only interpreted at runtime. To get your IDE to know about your library, you will have to add it to the Python Path, e.g. like described in this question: https://stackoverflow.com/a/55209725/5660315.

Within a larger PyCharm or IntelliJ project is there support for subdirectories with their own virtualenvs?

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.

Eclipse and Pydev integration for Edx souce code

We are keen to use PyDev for our edX customizatoin and we want to configure edX code on it. Do you know of any documentation which talks about how to configure PyDev for edX code? Basically, I have imported all the projects of edX into PyDev but I am stuck at the point of building the projects as I am not aware of the configurations required to be done and there are lot of dependancies between various projects. Any help would be appreciated. Thanks & Regards,
Abhijeet Mote
I happen to know where to find the directions for configuring Eclipse.
You can find the details at the attachment of this post.
Edit: Update based on the configuration from the link
Create MITx python environment
Go to Windows>Preferences>PyDev>Interpreter - Python (Ubuntu) or Eclipse>Preferences>PyDev>Interpreter - Python (Mac)
Set up (or create new) location, pointing to ~/mitx_all/python/bin/python. You may need to spell out the home path. You may get a warning saying that source code is not found.
(If necessary, add /usr/bin/python2.7 to list of included paths for interpreter. That might provide some of the missing source code.)
Create MITx project
Do File>New>Other, select PyDev/PyDev Django Project
Enter "mitx" or similar as the project name and point to mitx_all/mitx location
In project properties (right click the project in left panel), set PyDev - PYTHONPATH, Source Folders to:
the root dir /mitx
/mitx/common/lib/xmodule
/mitx/cms
/mitx/cms/djangoapps
/mitx/lms
/mitx/lms/djangoapps
/mitx/lms/lib
/mitx/common/djangoapps
/mitx/common/lib
Create a manage.py file to use that's within the project hierarchy (instead of django_admin.py, which is over in mitx_all/python/bin). For example, put the following in ~/mitx_all/mitx/manage.py:
#!/home/<username>/mitx_all/python/bin/python
from django.core import management
if __name__ == "__main__":
management.execute_from_command_line()
Open Debug Configurations dialog. (Either right-click on project -> Debug as -> Debug Configurations or menu Run -> Debug Configurations to bring up configuration dialog.)
In dialog, right-click on PyDev Django -> New (New is the icon above the left nav).
On main tab, set name: pick a name to indicate what Django config you're using (e.g. LMS dev with runserver).
Project should be MITx (or whatever you called it)
For main module, browse to location of manage.py created above.
On Arguments tab, put program arguments. For example, to run the LMS, put "runserver --noreload --settings=lms.envs.dev --pythonpath=. 8000".
On Interpreter tab, select the MITx interpreter [which is in the drop down] (the one that uses mitx_all/python). This may be the same as your default interpreter, depending on your global interpreter settings.
In Environment tab, set DJANGO_SETTINGS_MODULE to point to the desired setting file (e.g. lms.envs.dev). If you aren't running with activate and such, then PATH may also need to be set (or augmented)

PyDev Package Explorer doesn't show files in Eclipse

I have been working for weeks using Eclipse's PyDev (Eclipse 3.8.1) and usually I click on files in Package Explorer to navigate through them. Now all of a sudden my Python project looks empty in the Package Explorer, just showing standard python libs.
I tried many things such as:
Refreshing project.
Importing again project to workspace.
Looking at custom filters in "customize view".
Opening project file in the editor and then using "link with editor".
Closing PyDev Package Explorer and opening it again.
Closing and opening Eclipse again (several times).
None of those showed the files. I don't know what is wrong with this project. I think it is not related, but it is also a git project.
Do you know what else is missing for me to try?
Thanks.
Found the solution in PyDev FAQ
The usual checks are:
In the PyDev Package Explorer menu, is the top level elements pointing to working sets, if yes, change it to "Project".
In the PyDev Package Explorer menu, customize view, the content has both, PyDev Navigator Content and Resources checked?
In the PyDev Package Explorer menu, customize view, is it possible that the filters selected are hiding all your elements?
The first item solved my problem.
This is a workaround. You could create a new working set and select the projects that you have created and wish to see them in the PyDev Package Explorer. This trick worked for me.
Steps to create a working set :
Click on the inverted triangle in the PyDec Package explorer.
Select "Select Working Set"
Click New button.
Select Resources and click Next
Select the projects that you wish to have them seen on the explorer -> mention a name of the working set and click Finish button.

How to work within individual files rather than projects in PyCharm

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.

Categories

Resources