PyDev project's PYTHONPATH automatically renamed. How do I properly configure this? - python

I'm new to PyDev and fairly rusty in Python. Trying to get back into it with a simple reddit app first. So here's my setup:
I have 2 PyDev projects: reddit and pylons (reddit api.py imports from pylons).
When I go into any file in the reddit project I get "unresolved import" for anything that tries to import from pylons. In reddit's PYTHONPATH, I've tried adding /pylons and /pylons/pylons, but whenever I refresh the project, PyDev seems to rename my references to /reddit and /reddit/pylons.
How do I fix this? How do I properly add the pylons project into the PYTHONPATH of reddit?

The most proper way to include another project source into your PYTHONPATH is to make a reference from your project to another project. For this make next steps:
Choose your project in PyDev Package Explorer (usually tree-like panel on the left).
Press Alt + Enter.
Click Project References tab.
Check Pylons project in a tab content frame.
Note, that path with source of both projects must be added to python path for Eclipse to build references for them.
Note #2 Also when you install some new Python packages you need to reindex them at Window > Preferences > PyDev > Interpreter - Python.

Since Rostyslav's solution is not working for you, perhaps you should try to add pylons as an external library. At reddit's Properties window click PyDev-PYTHONPATH tab and the External Libraries tab. Click Add source folder and find pylons's source folder. Changes to external libraries are not monitored. So you have to use Force restore internal info when pydev can't find new references (tipically when you make changes to pylons's structure).

Related

Moving PyCharm projects folder and keeping reference to python interpreter

I'm using macOS 10.15.4 and PyCharm 2019.3.4
I currently have a folder inside the PyCharmProjects folder (that is automatically created when PyCharm first runs) where I keep projects for a class. I want to move this folder, which contains multiple other folders which each contain PyCharm projects, somewhere else on my computer (like the Desktop). The problem is if I move the folder (or even just a single PyCharm project) the next time I open the project in PyCharm, it says "Invalid python interpreter selected for the project." Now I can manually go into PyCharm preferences and point it to the new location I moved the folder so it can use the correct python interpreter. But this would be tedious to do for every single project I have. (And yes, every project I have uses its own interpreter and virtual environment.)
Is then a way I can move a folder containing multiple PyCharm projects without loosing the references to each of their respective python interpreters?
(Feel free to reword this question or the title.)
PyCharm uses configurations from your home directory. The docs say
macOS
Configuration
~/Library/Preferences/<PRODUCT><VERSION>
Caches
~/Library/Caches/<PRODUCT><VERSION>
Plugins
~/Library/Application Support/<PRODUCT><VERSION>
Logs
~/Library/Logs/<PRODUCT><VERSION>
where <PRODUCT> is PyCharm.
They are xml files. Among them also a list of configured interpreters. They are separate because they are available for all your projects to be chosen as the default interpreter. You'll have to edit the xml files.
For some reason PyCharm does not help with a moved virtualenv directory directly. (see comment from engineer: https://youtrack.jetbrains.com/issue/PY-32435#focus=Comments-27-3139072.0-0)
On the upside, everything else seems to work, except the path to the venv python. That one can be changed through preferences/project preferences/python interpreter. Just click on "add", "existing", and select the python compiler in your project folder.
Try not to select the ".virtualenvs" python compiler, as if you fumble like I did, it seems it's a bit of a pain to get rid of it and go back to the right one. If you do that, renaming the project folder once more gets you back to square one and you get to try again.
Note that I'm using MacOS. YMMV.

How to fix: Pytest "Create Test" dialog box won't open in Pycharm

I'm trying to follow the tutorial on jetbrains (link: https://www.jetbrains.com/help/pycharm/pytest.html) about using pytest in pycharm so I can work on test driven development. I'm stuck at the part where I'm supposed to create a test for a specific function in the Car.py module (found here: https://www.jetbrains.com/help/pycharm/creating-and-running-your-first-python-project.html#) by hitting ctrl+shift+T and then clicking "Create new test...".
Screenshot of the step in the tutorial I'm stuck at.
When I click "Create new test..." nothing happens. The dialog box which is supposed to pop-up does not appear.
I've gone to Settings>Tools>Python Integrated Tools and changed Default test runner to pytest like I'm supposed to. I checked packages in the Project Interpreter and pytest version 5.3.5 is there which I just installed. I restarted pycharm for good measure. Does anyone know why the dialog box might not be appearing? I'm sure there is a more manual approach to making the test file but the built-in pycharm ability to do this quicker/easier is preferable.
Thanks for answering your own question, you have given me an idea for my solution.
For what reason so ever, I was not providing PyCharm a root directory for my project so it was not recognising my files.
I changed it under:
Preferences > Project:"your project name" > Project Structure
It was a silly mistake in my case. I was not opening the proper project folder. Instead I was opening parent folder.
My hierarchy was
E:\Projects\SampleProject
Instead of opening SampleProject in PyCharm, I was opening Projects directory whole.
I think PyCharm will only open this if it recognizes valid structure of the project.
Press Ctrl+Alt+S to open the Settings window, go to Project <project name> > Project Structure and mark your source folders as Sources (highlighted as blue in my version of PyCharm). Then you should be able to create tests for them.

Moving from Eclipse to PyCharm: Adding a project (its folder) to the python path

My apologies in advance if I am not very clear (I am still programming in a rather childlish way).
I am trying to move from Eclipse to PyCharm (community version) and so far no issues with the editor or cloning from github. However I am having doubts on how to use classes and modules from an external project:
In Eclipse I would go to: Windows -> Preferences -> Interpreter -> Python Interpreter and over here add new folder, which would be the subfolder within Eclipse "workspace" folder where I am saving the project.
In PyCharm I am using File -> Preferences -> Project: Project_Name1 and over here I have the possibility to mark as sources (or excluded) all the project folders I want.
This brings me many doubts...
I imported several repositories (my own) from github and I set the option to keep open in the same window as the first (this option is new compared with eclipse but it gives me the same output with all the project within the package explorer).
However, when I got to File -> Preferences -> Project: Only the first project appears independently which one I have selected. How can I modify the interpreter and/or dependencies for the rest?
If I add a project folder to the project structure do I also need to modify the project dependencies?
Incidentally any guide or tutorial on the right so it imports are not broken with each new IDE or when the code is shared would be most welcomed.
Here is a view of my Setting:
It Shows only the name of first project, but when you go to each setting under the project, the setting is shown for all the projects. When I go under Project Interpreter for example, I can choose the Interpreter for each project separately:
Is it different for you?

How open existing PyDev project?

Experienced Programmer, but new to Python. Using OsGeoLive and PyDev on Eclipse.
A few weeks ago I started a new project in PyDev (my first one). I set the project aside and am only now coming back to it. But when I open Eclipse, I do not see any way to open the project I started. There is no Recent Projects on the File Menu, File > Import does not seem to be what I need. The Project menu has an Open Project item, but it is deactivated. When I open the project's .xml file from Eclipse, it opens it as an editable XML file.
How do I open an existing PyDev project, or alternately, how do I trouble shoot PyDev losing track of the directory structure?
Locate the Package Explorer window and see if there any project folders in there. You also have the option to see "Working Sets", which are convenient to create projects distributed through several folders. Just to make sure, check out if you see something like this (You can display Projects with the prompted submenu):
If you don't see anything there, perhaps your project folder is missing an init.py file, in order to be recognized as a package, which is a general Python standard.
Sorry if this doesn't help much, I cannot leave comments yet.

PyCharm import external library

I am using PyCharm as an editor for python code in Houdini. Whenever I try to import the main Houdini library (hou) I get an error flagged in PyCharm. If I include the code snippet:-
try:
import hou
except ImportError:
# Add $HFS/houdini/python2.6libs to sys.path so Python can find the
# hou module.
sys.path.append(os.environ['HFS'] + "/houdini/python%d.%dlibs" % sys.version_info[:2])
import hou
my code executes, without problem, from both Houdini and my selected interpreter.
My problem is with PyCharm itself. The editor flags 'import hou' as an error and any subsequent files that import this file flag modules imported by this file as errors as well. Hence I loose type ahead functionality and get an over abundance of error messages that make it hard to spot the real problems.
How do I get PyCharm to recognize the path to the hou module.
I have tried, for a couple of days, to Google a solution to this problem but they all seem to refer to tabs and settings that are not in my version of PyCharm (Community Edition 3.4.1). E.G. my 'Project Interpreter' setting only has a list of Packages and has no 'path' tab as stated in many 'fixes' to closely related problems.
Since PyCharm 3.4 the path tab in the 'Project Interpreter' settings has been replaced. In order to add paths to a project you need to select the cogwheel, click on 'More...' and then select the "Show path for the selected interpreter" icon. This allows you to add paths to your project as before.
My project is now behaving as I would expect.
Answer for PyCharm 2016.1 on OSX:
(This is an update to the answer by #GeorgeWilliams993's answer above, but I don't have the rep yet to make comments.)
Go to Pycharm menu --> Preferences --> Project: (projectname) --> Project Interpreter
At the top is a popup for "Project Interpreter," and to the right of it is a button with ellipses (...) - click on this button for a different popup and choose "More" (or, as it turns out, click on the main popup and choose "Show All").
This shows a list of interpreters, with one selected. At the bottom of the screen are a set of tools... pick the rightmost one:
Now you should see all the paths pycharm is searching to find imports, and you can use the "+" button at the bottom to add a new path.
I think the most significant difference from #GeorgeWilliams993's answer is that the gear button has been replaced by a set of ellipses. That threw me off.
Update (2018-01-06): This answer is obsolete. Modern versions of PyCharm provide Paths via Settings ⇨ Project Interpreter ⇨ ⚙ ⇨ Show All ⇨ Show paths button.
PyCharm Professional Edition has the Paths tab in Python Interpreters settings, but Community Edition apparently doesn't have it.
As a workaround, you can create a symlink for your imported library under your project's root.
For example:
myproject
mypackage
__init__.py
third_party -> /some/other/directory/third_party
updated on May 26-2018
If the external library is in a folder that is under the project then
File -> Settings -> Project -> Project structure -> select the folder and Mark as Sources!
If not, add content root, and do similar things.
In my case, the correct menu path was:
File > Default settings > Project Interpreter
I wanted to add an import path, for another project elsewhere in my workspace.
MacOS Catalina 10.15.5
PyCharm Community 2020.1.1
PyCharm - Preferences - Project interpreter - Cog symbol - Show All
At the bottom of that dialog, it shows 5 buttons:
Plus, Minus, Pencil, Funnel, and Directory tree.
Click Directory tree. You can now use the Plus button in the new dialog to add your 'external library' search path.
If successful, you should now see the directory name in the "External Libraries" pane in the Project panel.
In order to reference an external library in a project
File -> Settings -> Project -> Project structure -> select the folder and mark as a source

Categories