Add directory to Python path in PyCharm? - python

I want to be able to use the paraview.simple library in PyCharm. I already have paraview installed in my computer. This package cannot be installed with pip and there are no .whl files as far as I can tell. The website docs recommend that the directory containing all the python files be added to PYTHONPATH.
How do I add the relevant folder in PYTHONPATH on my PyCharm session, and keep it there by default, such that when I close out and reopen the paraview.simple library is still available?

You can add custom paths this way.
Go to File->Settings->project Interpreter
In the Project-Interpreter field, click the down facing arrow and select "show All"
In that Menu, highlight your interpreter and then in the right menu, select the button "Show paths for the selected interpreter" (this is the last button)
click the plus symbol to add your path

Adding interpreter paths in PyCharm:
Project Settings/Project Interpreter: select "settings" icon
Project Interpreters: select "tree" icon
Interpreter Paths: select "plus" icon

In PyCharm version 2020.3.1 use the following workflow instead (see official docs):
Ctrl+Alt+S - open Settings.
File|Settings|Project|Project Structure...
You will see project folder structure. Mark subfolders as Sources or Excluded

Use anaconda with Pycharm
To install paraview with anaconda run the following command: conda install -c conda-forge paraview
If conda command is not recognized then register the Anaconda path to environment variable
Now open pycharm and give the configuration path as the Anaconda path
File -> Settings -> Project Interpreter
In Project Interpreter give similar path : "C:\Users\username\Anaconda3\python.exe"

For me this solved it: mapping the local path to the remote inside the Pycharm run configurations settings.
Edit Configurations > Path Mappings >
Local path = /<Users/me/pycharm etc.../project root>
remote path = /
eg.

Related

Already Installed package on cmd then how it work on pycharm?

I want to install one package on cmd then it also work on pycharm. No need to download or install it from pycharm. How it is possible? Please help me. I already installed that package on cmd.
Adding a Path
Go into File > Settings > Project Settings > Project Interpreter.
Then press configure interpreter, and navigate to the "Paths" tab.
pycharm path tab
Press the + button in the Paths area. You can put the path to the module you'd like it to recognize.
But I don't know the path..
Open the python interpreter where you can import the module.
>> import package_name
>> package_name.__file__
"path/to/package_name"
Most commonly you'll have a folder structure like this:
foobarbaz/
Package_name/
__init__.py
other_file.py
You want to add foobarbaz to the path here.
Assumption:
Initial installation of Python, Add Python to Path . was checked.
Initial installation of PyCharm.
A package is installed using command line pip install package.
Result:
Package is installed in the default installation of Python.
Next:
Start a new PyCharm project.
Select File menu |Settings
Check the Base interpreter, it will likely be the python.exe file in the default Python installation. If not, locate the python.exe file and select it.
Check Inherit global-site packages.
Click Create Button
Result: The project will be created with installed packages.

ModuleNotFoundError: No module named 'pyshark' [duplicate]

In PyCharm, I've added the Python environment /usr/bin/python. However,
from gnuradio import gr
fails as an undefined reference. However, it works fine in the Python interpreter from the command line.
GNURadio works fine with python outside of Pycharm. Everything is installed and configured how I want it.
Gnuradio is located at /usr/local/lib/python2.7/site-packages/gnuradio
Also:
PYTHONPATH=/usr/local/lib/python2.7/site-packages:/usr/local/lib/python2.7/site-packages/gnuradio
Adding a Path
Go into File → Settings → Project Settings → Project Interpreter.
Then press configure interpreter, and navigate to the "Paths" tab.
Press the + button in the Paths area. You can put the path to the module you'd like it to recognize.
But I don't know the path..
Open the python interpreter where you can import the module.
>> import gnuradio
>> gnuradio.__file__
"path/to/gnuradio"
Most commonly you'll have a folder structure like this:
foobarbaz/
gnuradio/
__init__.py
other_file.py
You want to add foobarbaz to the path here.
You should never need to modify the path directly, either through environment variables or sys.path. Whether you use the os (ex. apt-get), or pip in a virtualenv, packages will be installed to a location already on the path.
In your example, GNU Radio is installed to the system Python 2's standard site-packages location, which is already in the path. Pointing PyCharm at the correct interpreter is enough; if it isn't there is something else wrong that isn't apparent. It may be that /usr/bin/python does not point to the same interpreter that GNU Radio was installed in; try pointing specifically at the python2.7 binary. Or, PyCharm used to be somewhat bad at detecting packages; File > Invalidate Caches > Invalidate and Restart would tell it to rescan.
This answer will cover how you should set up a project environment, install packages in different scenarios, and configure PyCharm. I refer multiple times to the Python Packaging User Guide, written by the same group that maintains the official Python packaging tools.
The correct way to develop a Python application is with a virtualenv. Packages and version are installed without affecting the system or other projects. PyCharm has a built-in interface to create a virtualenv and install packages. Or you can create it from the command line and then point PyCharm at it.
$ cd MyProject
$ python2 -m virtualenv env
$ . env/bin/activate
$ pip install -U pip setuptools # get the latest versions
$ pip install flask # install other packages
In your PyCharm project, go to File > Settings > Project > Project Interpreter. If you used virtualenvwrapper or PyCharm to create the env, then it should show up in the menu. If not, click the gear, choose Add Local, and locate the Python binary in the env. PyCharm will display all the packages in the selected env.
In some cases, such as with GNU Radio, there is no package to install with pip, the package was installed system-wide when you install the rest of GNU Radio (ex. apt-get install gnuradio). In this case, you should still use a virtualenv, but you'll need to make it aware of this system package.
$ python2 -m virtualenv --system-site-packages env
Unfortunately it looks a little messy, because all system packages will now appear in your env, but they are just links, you can still safely install or upgrade packages without affecting the system.
In some cases, you will have multiple local packages you're developing, and will want one project to use the other package. In this case you might think you have to add the local package to the other project's path, but this is not the case. You should install your package in development mode. All this requires is adding a setup.py file to your package, which will be required anyway to properly distribute and deploy the package later.
Minimal setup.py for your first project:
from setuptools import setup, find_packages
setup(
name='mypackage',
version='0.1',
packages=find_packages(),
)
Then install it in your second project's env:
$ pip install -e /path/to/first/project
For me, it was just a matter of marking the directory as a source root.
Add path in PyCharm 2017
File -> Settings (or Ctrl+Alt+S) -> Project -> Project Interpreter
Show all
Select bottom icon on the right side
Click on the plus button to add new path to your module
My version is PyCharm Professional edition 3.4, and the Adding a Path part is different.
You can go to "Preferences" --> "Project Interpreter". Choose the tool button at the right top corner.
Then choose "More..." --> "Show path for the selected interpreter" --> "Add". Then you can add a path.
DON'T change the interpreter path.
Change the project structure instead:
File -> Settings -> Project -> Project structure -> Add content root
In PyCharm 2020.1 CE and Professional, you can add a path to your project's Python interpreter by doing the following:
1) Click the interpreter in the bottom right corner of the project and select 'Interpreter Settings'
2) Click the settings button to the right of the interpreter name and select 'Show All':
3) Make sure your project's interpreter is selected and click the fifth button in the bottom toolbar, 'show paths for the selected interpreter':
4) Click the '+' button in the bottom toolbar and add a path to the folder containing your module:
For PyCharm Community Edition 2016.3.2 it is:
"Project Interpreter" -> Top right settings icon -> "More".
Then on the right side there should be a packages icon. When hovering over it it should say "Show paths for selected interpreter". Click it.
Then click the "Add" button or press "alt+insert" to add a new path.
As quick n dirty fix, this worked for me:
Adding this 2 lines before the problematic import:
import sys
sys.path.append('C:\\Python27\\Lib\site-packages')
On Project Explorer, you can right click on the folder where the module is contained and set as 'Source'.
It will be parsed in the Index for code completion as well as other items.
I'm new to PyCharm (using 2018.3.4 CE) and Python so I rotely tried to follow each of the above suggestions to access the PIL (Pillow) package which I knew was in system-site-packages. None worked. I was about to give up for the night when I happened to notice the venv/pyvenv.cfg file under my project in the Project Explorer window. I found the line "include-system-site-packages = false" in that file and so I changed it to "true". Problem solved.
In my PyCharm 2019.3, select the project, then File ---> Settings, then Project: YourProjectName, in 'Project Interpreter', click the interpreter or settings, ---> Show all... ---> Select the current interpreter ---> Show paths for the selected interpreter ---> then click 'Add' to add your library, in my case, it is a wheel package
For me there is also another issue. If you try to add a folder that in the past had a .idea folder, but your current project has it's own .idea folder your pycharm might get confused for some reason -- even if you have the right python/conda env. For me deleting the .idea folder of the other project fixed the confusion that it could find the obviously correctly installed pkgs. Then it was it was able to find them in the pycharm editor GUI snf stopped underlyinging them in red.
Download anaconda
https://anaconda.org/
once done installing anaconda...
Go into Settings -> Project Settings -> Project Interpreter.
Then navigate to the "Paths" tab and search for /anaconda/bin/python
click apply

Activating Anaconda Environment in VsCode

I have Anaconda working on my system and VsCode working, but how do I get VsCode to activate a specific environment when running my python script?
Simply use
shift + cmd + P
Search Select Interpreter
Select it and it will show you the list of your virtual environment created via conda and other python versions
select the environment and you are ready to go.
Quoting the 'Select and activate an environment' docs
Selecting an interpreter from the list adds an entry for python.pythonPath with
the path to the interpreter inside your Workspace Settings.
If Anaconda is your default Python install then it just works if you install the Microsoft Python extension.
The following should work regardless of Python editor or if you need to point to a specific install:
In settings.json edit python.path with something like
"python.pythonPath": "C:\\Anaconda3\\envs\\py34\\python.exe"
Instructions to edit settings.json
Setting python.pythonPath in VSCode's settings.json file doesn't work for me, but another method does. According to the Anaconda documentation at Microsoft Visual Studio Code (VS Code):
When you launch VS Code from Navigator, VS Code is configured to use the Python interpreter in the currently selected environment.
The best option I found is to set the python.venvPath parameter in vscode settings to your anaconda envs folder.
"python.venvPath": "/Users/[...]/Anaconda3/envs"
Then if you bring up the command palette (ctl + shift + P on windows/linux, cmd + shift + P on mac) and type Python: Select Workspace Interpreter all your envs will show up and you can select which env to use.
The python extension will also need to be installed for the Select Workspace Interpreter option.
Note: The Select Workspace Interpreter takes around 10 seconds to come up on my computer using the current version of VSCode.
Although approved answer is correct, I want to show a bit different approach (based on this answer).
Vscode can automatically choose correct anaconda environment if you start vscode from it. Just add to user/workspace settings:
{
"python.pythonPath": "C:/<proper anaconda path>/Anaconda3/envs/${env:CONDA_DEFAULT_ENV}/python"
}
It works on Windows, macOS and probably Unix. Further read on variable substitution in vscode: here.
Unfortunately, this does not work on macOS. Despite the fact that I have export CONDA_DEFAULT_ENV='$HOME/anaconda3/envs/dev' in my .zshrc and "python.pythonPath": "${env.CONDA_DEFAULT_ENV}/bin/python",
in my VSCode prefs, the built-in terminal does not use that environment's Python, even if I have started VSCode from the command line where that variable is set.
Just launch the VS Code from the Anaconda Navigator. It works for me.
Find a note here: https://code.visualstudio.com/docs/python/environments#_conda-environments
As noted earlier, the Python extension automatically detects existing
conda environments provided that the environment contains a Python
interpreter. For example, the following command creates a conda
environment with the Python 3.4 interpreter and several libraries,
which VS Code then shows in the list of available interpreters:
conda create -n env-01 python=3.4 scipy=0.15.0 astroid babel
In contrast, if you fail to specify an interpreter, as with conda create
--name env-00, the environment won't appear in the list.
If you need an independent environment for your project:
Install your environment to your project folder using the --prefix option:
conda create --prefix C:\your\workspace\root\awesomeEnv\ python=3
In VSCode launch.json configuration set your "pythonPath" to:
"pythonPath":"${workspaceRoot}/awesomeEnv/python.exe"
Python Path is now deprecated and now you should set Conda Path instead. This way you can pick different environements on the fly.
Click ctrl + , then search for Conda Path and add absolute path to script, e.g.:
C:\Users\{myUser}\miniconda3\Scripts\conda.exe
Pick specific environment for each project in bottom left corner or through Command Pallete (ctrl + Shift + P -> search Python: Select Interpreter)
The simplest way to do it -
First open up terminal or command line and navigate to the project directory where you created the virtual environment.
Then activate the virtual environment with the command
conda activate venv_name
Once activated, in terminal type -
code .
This will open the vscode with the activated virtual environment. Look at the bottom of the pic. The dot after code . tells terminal to open the current directory in vscode.
I found out that if we do not specify which python version we want the environment which is created is completely empty. Thus, to resolve this issue what I did is that I gave the python version as well. i.e
conda create --name env_name python=3.6
so what it does now is that it installs python 3.6 and now we can select the interpreter. For that follow the below-mentioned steps:
Firstly, open the command palette using Ctrl + Shift + P
Secondly, Select Python: select Interpreter
Now, Select Enter interpreter path
We have to add the path where the env is, the default location will be
C:\Users\YourUserName\Anaconda3\envs\env_name
Finally, you have successfully activated your environment.
It might now be the best way but it worked for me. Let me know if there is any issue.
I found a hacky solution replace your environment variable for the original python file so instead it can just call from the python.exe from your anaconda folder, so when you reference python it will reference anaconda's python.
So your only python path in env var should be like:
"C:\Anaconda3\envs\py34\", or wherever the python executable lives
If you need more details I don't mind explaining. :)
As I was not able to solve my problem by suggested ways, I will share how I fixed it.
First of all, even if I was able to activate an environment, the corresponding environment folder was not present in C:\ProgramData\Anaconda3\envs directory.
So I created a new anaconda environment using Anaconda prompt,
a new folder named same as your given environment name will be created in the envs folder.
Next, I activated that environment in Anaconda prompt.
Installed python with conda install python command.
Then on anaconda navigator, selected the newly created environment in the 'Applications on' menu.
Launched vscode through Anaconda navigator.
Now as suggested by other answers, in vscode, opened command palette with Ctrl + Shift + P keyboard shortcut.
Searched and selected Python: Select Interpreter
If the interpreter with newly created environment isn't listed out there, select Enter Interpreter Path and choose the newly created python.exe which is located similar to C:\ProgramData\Anaconda3\envs\<your-new-env>\ .
So the total path will look like C:\ProgramData\Anaconda3\envs\<your-nev-env>\python.exe
Next time onwards the interpreter will be automatically listed among other interpreters.
Now you might see your selected conda environment at bottom left side in vscode.
"python.pythonPath" is deprecated, quote from vscode:
The "python.pythonPath" setting in your settings.json is no longer
used by the Python extension. If you want, you can use a new setting
called "python.defaultInterpreterPath" instead. Keep in mind that you
need to change the value of this setting manually as the Python
extension doesn’t modify it when you change interpreters. Learn more.
Thus, IF you want to assign the path manually (not reccommended, as explained above), open the "settings.json" of your workspace or the default one and use
{
"python.defaultInterpreterPath": "C:\\Users\\MYUSER\\anaconda3\\envs\\testenv\\python.exe"
}

Pycharm Jupyter Notebook `PYTHONPATH`?

System information
Mac OS X 10.12 Sierra
Pycharm 2016.2.3
Python 3.5.1
Jupyter 4.1.0
Question
When I'm using Jupyter Notebook under Pycharm, it doesn't seem to include my current working directory, or the source root directory, in the PYTHONPATH of the currently active Jupyter Notebook. This is unlike the behaviour of ordinary .py files, for which Pycharm automatically includes its current directory and the project source root directory in the PYTHONPATH. So, is it possible to set Pycharm to automatically include the project source root directory in the PYTHONPATH for Jupyter Notebooks run under Pycharm?
It turns out, in a past endeavour, I modified my jupyter_notebook_config.py in %USER%\.jupyter folder. Looked at lines as follows:
## The directory to use for notebooks and kernels.
c.NotebookApp.notebook_dir = 'D:\\Works\\GitHub\\Jupyter\\'
The line under the ##, indicates the override path, evidently.
When I commented it out...Pycharm all of the sudden successfully launched with pwd of the file (location of the .ipynb). good luck, hth.
Pycharm 2019.1 has done an overhaul of Jupyter support. Jetbrains says this in reference to Pycharm 2019.1: "Jupyter Notebook support in PyCharm was redesigned from the ground up."
So, as of Pycharm 2019.1, you can now set Pycharm to include the project source root directory for Jupyter Notebooks.
See this page of Pycharm 2019.1 help.
There's a bunch of ways to mess with the environment, but the PyCharm specific way that worked for me was to make the directory with my source in it a PyCharm "sources root" as described here: https://www.jetbrains.com/help/pycharm/content-root.html
This can be done from the context menu of a directory in the navigator, or from the "Project Structure" settings pane.
This seems to update the PYTHONPATH as well as sorting out things like autocomplete.
For anyone using the free version of PyCharm, which has no direct Jupyter Notebook support, there's a simple workaround:
Go to PyCharm "Terminal" tab at the bottom (NOT to be confused with the Python console tab!)
[NOTE: steps 2 and 3 are for Window7; on other OS's, just research how to set environment variables thru the terminal or command prompt]
Issue the command:
set PYTHONPATH=\path_to_your_project_root_folder\
EXAMPLE on Windows: set PYTHONPATH=\Docs\- MY CODE\my_project_root_folder\
Optionally, you may verify that the environmental variable got set by issuing the command: set PYTHONPATH
Issue the command: jupyter notebook
That's all! A new browser tab will open up... and, insider any notebook, you can verify the path by issuing the python commands:
import sys
sys.path

Configuring PyCharm with existing virtualenv

I am trying to get PyCharm running using existing virtualenv setting. I have pointed my PyCharm project to python interpreter in existing virtualenv ~/.virtualenvs/myproj/ in the following path
File -> Default Settings -> Default Project -> Python Interpreter
the project runs fine but the editor is still glowing RED on packages installed as part of the virtualenv.
Any idea what I am missing?
To run PyCharm properly for your project, you need to set Python Interpreter and Python Structure correctly.
I had set Python Interpreter correctly but missed out on Python Structure.
Go to Pycharm->Preferences->your_project->Project Structure
Add the right content root
It has nothing to do with your working directory which you can set separately in your debug/run configuration
also don't forget to add environment variables you need and you should be good to go.
For Intellij Idea 2016.2, following is the path to add site-packages installed in virtualenv
File -> Project Structure -> Sources -> "Use + button and add as Sources"
if hidden directory may is be visible, you may either change your view settings or copy paste the path to site-packages in virtualenv
Dont click on make available to all projects and it will work. Otherwise it would give error saying -- specify a different sdk name
I did what was specified by comiventor in the accepted answer, but also had to do what Brian W commented: mark the root folder as a "Sources Root". This is done as follows:
Right click on your root directory
Look at the bottom for the option Mark Directory as
Choose Sources Root (the folder icon color should change from gray to blue)
That's all!
For PyCharm 19.3
Ctrl + Alt + S or File / Settings
then
Project: your-name-project / Project Interpreter / Gear Icon / Add ...
Choose New environment
in Location:
set your path / to / venv
Finally, you should see
Project Interpreter : path / to / venv / Scripts / python.exe
For Ubuntu users:
Step 1: Go to: "File > Settings > Python Interpreter"
Step 2: Drop down the Python Interpreter options and select "Show All..."
Step 3: Click on the left top "+" symbol to add your virtual environment
Step 4: Under Virtual Environment, select "Existing environment"
Step 5: By clicking the three dots at right side, link to the python
interpreter of your virtual environment.
For me, the linking looks like this: "/home/varat/cv/bin/python3"
My virtual environment name is "cv"
Step 6: Finally, click "OK"
After some loading, the pycharm will be working under your linked virtual environment.

Categories