I have a custom anaconda environment which I am using to run a program which is dependent on files in another directory. How do I add these files to the environment like I would do with a system interpreter by adding the paths to PYTHONPATH variable in system variables?
I did not need to add the custom files to the virtual environment configuration. I was using the Pycharm Python editor and found a better workaround by adding the paths to the selected interpreter (which in this case was the interpreter for the virtual python environment that I was using).
Go to File —>Settings—> Project: Project name—>Project Interpreter
Next to the project interpreter, click the little gear icon.
Click on Show all. A window should pop-up.
On the right side of the window, there’s a toolbar containing 5 options starting with the plus icon.
Click on the last icon with the file symbol. Another window should open.
Here click on the plus symbol on the top right toolbar.
Go to the directory that you want to include to use in your code and click Ok to add them to the interpreter path.
Repeat for any other directories or files that you may need to have.
This solution with Pycharm worked out pretty well for me since I only needed to reference those files on my computer for that particular project.
Related
I have a directory that I use for my PYTHONPATH: c:\test\my\scripts\. In this directory, I have some modules I import. It works well in my Python shell.
How do I add this directory path to PyCharm, so I can import what is in that directory?
Out of data, see Duane's answer below.
You need to go to the Main PyCharm Preferences, which will open up a separate window. In the left pane, choose Project:... > Project Interpreter. Now, in the main pane on the right, click the settings symbol (gear symbol) next to the field for "Project Interpreter". Choose More or Show All in the menu that pops up. Now in the final step, pick the interpreter you are using for this project and click on the tree symbol at the bottom of the window (hovering over the symbol reveals it as "Show paths for the selected interpreter"). Add your path by click in the "plus" symbol.
It took me ages to find, so I hope the detailed instructions will help. Further details are available in the PyCharm docs.
It is good practice to have __init__.py in each subfolder of the module you are looking to add, as well as making your project folder a 'Source Root'. Simply right-click on the folder in the path bar and choose 'Mark Directory as ...'
For Pycharm Community 2019.3
File
Settings
Project
Project Interpreter
Cog in top right => Show all
Select the interpreter
On the right toolbar, there are 5 icons, bottom one looks like a folder tree, click it
Press plus +
Select directory, press OK
In PyCharm Community 2019.2/2019.3 (and probably other versions), you can simply:
right-click any folder in your project
select "Mark Directory As"
select "Sources Root"
Modules within that folder will now be available for import. Any number of folders can be so marked.
The following answer is for PyCharm 2022.1.3+ (Professional Edition).
Open up Preferences (mac: ⌘ + ,)
Click on Project and then Project Interpreter
Click on the three vertical dots and the on Show All...
Choose your interpreter and then click on the directory structure icon
Click on the + icon and add the path you need to add to PYTHONPATH.
That's it!
enter image description here
you can go to setting in pycharm
and set interpreter python
I have been using Pycharm for years and have never had any problem. However, after my most recent PyCharm update I can no longer configure the interpreter.
Also each time I create a new project it creates a vent directory under my project. When I go to File/Default Settings/Project Interpreter, I am provided with new options.
In this window it allows you to configure a virtual environment, the conda environment, and the system interpreter. I am assuming that I should configure the system interpreter. From there I point PyCharm to the interpreter on my Mac at /usr/local/Cellar/python3/3.6.3/bin/python3 and hit OK.
It then takes me back to the main window where it shows the path in the project interpreter. At this point I hit apply and get a message:
Cannot Save Settings please use a different SDK name
It doesn't matter which interpreter I choose, I get the same message. Has anyone else come up with the same problem and how do I fix this?
Interestingly my old projects still work correctly.
I had the same problem while setting up the virtual environment for my project and no matter if I create a new virtual environment or select an existing one, I get the warning:
"Cannot Save Settings please use a different SDK name"
Finally I found the solution:
Click on the project interpreter dropdown and select show all.... There you might be having multiple virtual environments with same name. Now here is the conflict you need to fix manually by renaming them so every item has the unique name.
I ran into this issue when trying to get docker up and running with Pycharm 2018.1 and using the container's Interpreter. I would get the error below.
"Cannot Save Settings please use a different SDK name"
The issue I had was due to having multiple python interpreters of the same name.
Under Pycharm || Preferences || Project Interpreter
Click "show all" within the Project Interpreter dropdown and then delete any / all interpreters that you don't need.
How fix this in Windows 10:
close Pycharm .
delete this file: C:\Users\<username>\.PyCharmCE2018.3\config\options\jdk.table.xml
open Pycahrm again and load all python interceptors again.
Jon; I too noticed the difference in the latest version of PyCharm. It seems that it now has an affinity for the latest tool 'venv'. When you create an interpreter - that is what you get.
You can still create a virtualenv manually and point it there instead. I haven't looked into pointing it to a base python-bin because (frankly) you shouldn't generally do it.
My advice (and my proposed answer) is to embrace the change and let it create a 'venv' for you. From there, do all of your installs by "alt-F12". That will open a console with your virtual environment activate -- so your PIP installs into the virtual environment.
Once I began using virtual environments, I never looked back. It has made life much easier in the python world. Pycharm seems to know this as well and is trying to really encourage you to do the right thing.
SteveJ
[Edit Alternate way to get to terminal]
This may be a known issue of PyCharm and using the method of the accepted answer may not resolve it. Please see https://youtrack.jetbrains.com/issue/PY-27251.
You can close Pycharm and remove jdk.table.xml file from ~/Library/Preferences/.PyCharm2018.1/options directory (Mac version), then start PyCharm again.
Warning: By doing this, all your existing setup of python interpreters will be erased and you need to re-setup them again.
I had same error:
I deleted all invalid project interpreters ( they are red)
then I did https://stackoverflow.com/a/60583928/3842788
You cannot have 2 or more virtual environments with same name. Even if you have projects with same name stored at 2 different places, please give unique name to its venv. This will solve your problem.
To check all the virtual environments:
Go to File >> Settings >> Project: your_project_name >> Project Interpreter
And rename the venv name.
Go to Project > Project Interpreter > Select the dropdown menu > "Show All".
For me, there were several Python environments, two of which were red with an tag. Remove the envs that are red / have an tag, select the remaining valid one, and re-apply settings.
In my case, I moved my project to a different location and PyCharm started complaining about Cannot Save Settings please use a different SDK name. At the top of the main editor, it asks me to Configure Project Interpreter. I clicked it, and then ...
My solution
Remove all existing interpreters that are marked as invalid in the preference.
Select the interpreter in the moved venv subfolder in my project.
Without doing both, I kept getting the same "SDK name" error. It seemed that the project thinks that it already has an interpreter called "python.exe", if you don't actively remove all "invalid" ones.
I solved this issue by cleaning the Pycharm memory cache
I am working with Python 3.4.
When I use IDLE and start typing, e.g.,
my_main_folder = "C:/Us"
in the editor window, and then press Ctrl + Space, IDLE offers me all subfolders of C:, and if I press Tab, the string is completed to "C:/Users".
That means that IDLE (or Python Shell) is able to recognize a string as a path.
How to make PyCharm (Community edition) do that?
Its not able in Community Edition Pycharm. But it can be done in Professional Edition Pycharm. I would suggest you to move to Professional Edition.
Does PyCharm have autocomplete file path?
BUT,There is a work around for it in Community Edition.
Place caret in such place and press Alt+Enter -- see if you will have "Inject language or reference" option in appeared menu. If it's present -- use it and choose "File Reference" there.
NOTE 1: this option may not be available if string is empty or has less than 2 characters in it
NOTE 2: this is temp injection -- AFAIK it will be lost after IDE restart (and you will need to do it again if needed)
Otherwise: select desired file in Project View panel and use "Copy Reference" -- it will copy a file path relative to the project root -- you may only need to add leading "/"...
https://intellij-support.jetbrains.com/hc/en-us/community/posts/206574619-Autocompleting-relative-paths
After searching I found this answer:
can we tab through paths, in the same way you can in the terminal?
No, it's not posible:(
I wrote a plugin for Komodo called AutoCode that allowed me to ALT+Click on files in the Project view, and it would automatically resolve the right path
You can use 'Copy reference' in file right-click menu to copy file path relative to project root to clipboard. But this action doesn't unfortunately take resource roots into account
However, you can use CNTRL-SPACE twice and it will present a list of paths and files, example:
For a detailed description of this information see here
I am attempting to start a Tornado web server, but I need the Handlers to be able to import libraries from a custom path. I cannot simply add sys.path.append('..') when launching Tornado, so how do I do it?
import sys
sys.path.append('/home/user/py/lib')
To do this right you need to assign the PYTHONPATH to include the custom directories prior to the starting of the Tornado application.
The way to do this in Linux would be this:
PYTHONPATH=$PYTHONPATH:<custom path 1>:<custom path 2>:<etc>
I'm a bit rusty with Windows, but I believe in windows you would:
Windows XP
Right click 'My Computer'
Click on 'Properties'
Click on the 'Advanced' tab
Click 'Environment Variables'
In either the System or User Environment Variables locate the one
for PYTHONPATH. If one doesn't exist, create one in System. Add in
the new custom path to the PYTHONPATH. Make sure you use a ; and not
a : to separate the paths.
Windows Vista/7
Right click 'Computer' under the 'Start Button'.
Click on 'Properties'
Click on 'Advanced system settings' in the left pane
Click on the 'Advanced' tab
Click 'Environment Variables'
In either the System or User Environment Variables locate the one
for PYTHONPATH. If one doesn't exist, create one in System. Add in
the new custom path to the PYTHONPATH. Make sure you use a ; and not
a : to separate the paths.
I'm just learning SDL and I would like to know where is the correct place to put images and other file stuff in?
I use Visual C++ 2010 Express and the problem is that when I compile a program and start it straightly from IDE, it instantly stops because it can't load images. But when I start it by clicking on it in Release/Debug folder, where these images are placed, it is, of course, OK.
It seems that the IDE starts the program from another path than Release/Debug folder.
My question is - how shall I deal with it? Is there a way of setting the location of images directly in Visual C++? I'm asking this question because when I'm unable to start the program from IDE -> I can't use that IDE's debugger.
I hope I explained my situation well enough and thanks for the answers.
If you click on your project with the right mouse button, select properties, on the window that opens, click on "Debugging", notice the entry "Working directory", put there the directory where you want your program to run.
I am used to create a folder called work and put images, sounds, etc, there and configure the working directory to use this folder.
Also, any dlls not in system path must go there for your program to find it or in the same folder where visual is creating your exe.
Also on the same window, you will see the "Environment" entry, you can use it to configure path to dlls if neeeded setting the PATH environment variable, like for example:
PATH=c:\mylibs\dlls\