Debug python library - python

I have no idea how to debut a library without installing it, I want to enhance this library, but if I run PyCharm with this configuration:
Pycharm debug configuration
I never collaborated on github or similar, however I decided to join this Hacktoberfest. What configuration should I use? In case I have to install it, how can I put the breakpoints easily?
Edit for more info:
Package has 3 modules:
scdl
|
|-------- client.py
|--------------- scdl.py
|--------------------utils.py
When I run the scdl.py module which has a main it tries to import the client and utils modules, however It can't find them for some reason, I tried to add the directory where the scdl package is stored to the PYTHONPATH variable, and still got no result
Screenshot of PyCharm Directory
The error is:
from scdl import client, utils
ImportError: cannot import name 'client'

When working on something that's intended to be a module, I usually install the module in my interpretter then use the "module_name" run configuration in PyCharm instead of the "script_path" like you have set up.
Create a virtual environment: python -m venv venv
Activate the virtual environment: source venv/bin/activate
Follow the instructions in the projects readme to install (in this case python setup.py install)
Add that virtual environment to PyCharm (Preferences > Project Interpreter)
PyCharm will prompt you to install requirements, do it
Where it says "script_path" in your run configuration, click the little arrow and change to "module_name"
Set the module name to "scdl.scdl"
Now you should be able to run/debug as normal, hope that helps!

Related

No module named 'facebook' [duplicate]

I'm having trouble with using 'requests' module on my Mac. I use python34 and I installed 'requests' module via pip. I can verify this via running installation again and it'll show me that module is already installed.
15:49:29|mymac [~]:pip install requests
Requirement already satisfied (use --upgrade to upgrade): requests in /opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages
Although I can import 'requests' module via interactive Python interpreter, trying to execute 'import requests' in PyCharm yields error 'No module named requests'. I checked my PyCharm Python interpreter settings and (I believe) it's set to same python34 as used in my environment. However, I can't see 'requests' module listed in PyCharm either.
It's obvious that I'm missing something here. Can you guys advise where should I look or what should I fix in order to get this module working? I was living under impression that when I install module via pip in my environment, PyCharm will detect these changes. However, it seems something is broken on my side ...
In my case, using a pre-existing virtualenv did not work in the editor - all modules were marked as unresolved reference (running naturally works, as this is outside of the editor's config, just running an external process (not so easy for debugging)).
Turns out PyCharm did not add the site-packages directory... the fix is to manually add it.
On Pycharm professional 2022.3
Open File -> Settings -> Python Interpreter, open the drop-down and pick "Show All..." (to edit the config) (1), right click your interpreter (2), click "Show Interpreter Paths" (3).
In that screen, manually add the "site-packages" directory of the virtual environment [looks like .../venv/lib/python3.8/site-packages (4) (I've added the "Lib" also, for a good measure); once done and saved, they will turn up in the interpreter paths.
The other thing that won't hurt to do is select "Associate this virtual environment with the current project", in the interpreter's edit box.
If you are using PyCharms CE (Community Edition), then click on:
File->Default Settings->Project Interpreter
See the + sign at the bottom, click on it. It will open another dialog with a host of modules available. Select your package (e.g. requests) and PyCharm will do the rest.
This issue arises when the package you're using was installed outside of the environment (Anaconda or virtualenv, for example). In order to have PyCharm recognize packages installed outside of your particular environment, execute the following steps:
Go to
Preferences -> Project -> Project Interpreter -> 3 dots -> Show All ->
Select relevant interpreter -> click on tree icon Show paths for the selected interpreter
Now check what paths are available and add the path that points to the package installation directory outside of your environment to the interpreter paths.
To find a package location use:
$ pip show gym
Name: gym
Version: 0.13.0
Summary: The OpenAI Gym: A toolkit for developing and comparing your reinforcement learning agents.
Home-page: https://github.com/openai/gym
Author: OpenAI
Author-email: gym#openai.com
License: UNKNOWN
Location: /usr/local/lib/python3.7/site-packages
...
Add the path specified under Location to the interpreter paths, here
/usr/local/lib/python3.7/site-packages
Then, let indexing finish and perhaps additionally reopen your project.
Open python console of your pyCharm. Click on Rerun.
It will say something like following on the very first line
/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Applications/PyCharm.app/Contents/helpers/pydev/pydevconsole.py 52631 52632
in this scenario pyCharm is using following interpretor
/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
Now fire up console and run following command
sudo /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 -m pip install <name of the package>
This should install your package :)
Pycharm is unable to recognize installed local modules, since python interpreter selected is wrong. It should be the one, where your pip packages are installed i.e. virtual environment.
I had installed packages via pip in Windows. In Pycharm, they were neither detected nor any other Python interpreter was being shown (only python 3.6 is installed on my system).
I restarted the IDE. Now I was able to see python interpreter created in my virtual environment. Select that python interpreter and all your packages will be shown and detected. Enjoy!
Using dual python 2.7 and 3.4 with 2.7 as default, I've always used pip3 to install modules for the 3.4 interpreter, and pip to install modules for the 2.7 interpreter.
Try this:
pip3 install requests
This is because you have not selected two options while creating your project:-
** inherit global site packages
** make available to all projects
Now you need to create a new project and don't forget to tick these two options while selecting project interpreter.
The solution is easy (PyCharm 2021.2.3 Community Edition).
I'm on Windows but the user interface should be the same.
In the project tree, open External libraries > Python interpreter > venv > pyvenv.cfg.
Then change:
include-system-site-packages = false
to:
include-system-site-packages = true
Before going further, I want to point out how to configure a Python interpreter in PyCharm: [SO]: How to install Python using the "embeddable zip file" (#CristiFati's answer). Although the question is for Win, and has some particularities, configuring PyCharm is generic enough and should apply to any situation (with minor changes).
There are multiple possible reasons for this behavior.
1. Python instance mismatch
Happens when there are multiple Python instances (installed, VEnvs, Conda, custom built, ...) on a machine. Users think they're using one particular instance (with a set of properties (installed packages)), but in fact they are using another (with different properties), hence the confusion. It's harder to figure out things when the 2 instances have the same version (and somehow similar locations)
Happens mostly due to environmental configuration (whichever path comes 1st in ${PATH}, aliases (on Nix), ...)
It's not PyCharm specific (meaning that it's more generic, also happens outside it), but a typical PyCharm related example is different console interpreter and project interpreter, leading to confusion
The fix is to specify full paths (and pay attention to them) when using tools like Python, PIP, .... Check [SO]: How to install a package for a specific Python version on Windows 10? (#CristiFati's answer) for more details
This is precisely the reason why this question exists. There are 2 Python versions involved:
Project interpreter: /Library/Frameworks/Python.framework/Versions/3.4
Interpreter having the Requests module: /opt/local/Library/Frameworks/Python.framework/Versions/3.4
well, assuming the 2 paths are not somehow related (SymLinked), but in latest OSX versions that I had the chance to check (Catalina, Big Sur, Monterey) this doesn't happen (by default)
When dealing with this kind of error, it always helps (most likely) displaying the following information (in a script or interpreter console):
import os
import sys
print(sys.executable)
print(sys.version)
print(os.getcwd())
print(getattr(os, "uname", lambda: None)())
print(sys.path)
2. Python's module search mechanism misunderstanding
According to [Python.Docs]: Modules - The Module Search Path:
When a module named spam is imported, the interpreter first searches for a built-in module with that name. These module names are listed in sys.builtin_module_names. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:
The directory containing the input script (or the current directory when no file is specified).
PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
The installation-dependent default (by convention including a site-packages directory, handled by the site module).
A module might be located in the current dir, or its path might be added to ${PYTHONPATH}. That could trick users into making them believe that the module is actually installed in the current Python instance ('s site-packages). But, when running the current Python instance from a different dir (or with different ${PYTHONPATH}) the module would be missing, yielding lots of headaches
For a fix, check [SO]: How PyCharm imports differently than system command prompt (Windows) (#CristiFati's answer)
3. A PyCharm bug
Not very likely, but it could happen. An example (not related to this question): [SO]: PyCharm 2019.2 not showing Traceback on Exception (#CristiFati's answer)
To fix, follow one of the options from the above URL
4. A glitch
Not likely, but mentioning anyway. Due to some cause (e.g.: HW / SW failure), the system ended up in an inconsistent state, yielding all kinds of strange behaviors
Possible fixes:
Restart PyCharm
Restart the machine
Recreate the project (remove the .idea dir from the project)
Reset PyCharm settings: from menu select File -> Manage IDE Settings -> Restore Default Settings.... Check [JetBrains]: Configuring PyCharm settings or [JetBrains.IntelliJ-Support]: Changing IDE default directories used for config, plugins, and caches storage for more details
Reinstall PyCharm
Needless to say that the last 2 options should only be attempted as a last resort, and only by experts, as they might mess up other projects and not even fix the problem
Not directly related to the question, but posting:
[SO]: Run / Debug a Django application's UnitTests from the mouse right click context menu in PyCharm Community Edition? (a PyCharm related investigation from a while ago)
[SO]: ImportError: No module named win32com.client (#CristiFati's answer)
If you go to pycharm project interpreter -> clicked on one of the installed packages then hover -> you will see where pycharm is installing the packages. This is where you are supposed to have your package installed.
Now if you did sudo -H pip3 install <package>
pip3 installs it to different directory which is /usr/local/lib/site-packages
since it is different directory from what pycharm knows hence your package is not showing in pycharm.
Solution: just install the package using pycharm by going to File->Settings->Project->Project Interpreter -> click on (+) and search the package you want to install and just click ok.
-> you will be prompted package successfully installed and you will see it pycharm.
If any one faces the same problem that he/she installs the python packages but the PyCharm IDE doesn't shows these packages then following the following steps:
Go to the project in the left side of the PyCharm IDE then
Click on the venv library then
Open the pyvenv.cfg file in any editor then
Change this piece of code (include-system-site-packages = flase) from false to true
Then save it and close it and also close then pycharm then
Open PyCharm again and your problem is solved.
Thanks
This did my head in as well, and turns out, the only thing I needed to do is RESTART Pycharm. Sometimes after you've installed the pip, you can't load it into your project, even if the pip shows as installed in your Settings. Bummer.
For Anaconda:
Start Anaconda Navigator -> Enviroments -> "Your_Enviroment" -> Update Index -> Restart IDE.
Solved it for me.
After pip installing everything I needed. I went to the interpreter and re-pointed it back to where it was at already.
My case: python3.6 in /anaconda3/bin/python using virtualenv...
Additionally, before I hit the plus "+" sign to install a new package. I had to deselect the conda icon to the right of it. Seems like it would be the opposite, but only then did it recognize the packages I had/needed via query.
In my case the packages were installed via setup.py + easy_install, and the they ends up in *.egg directories in site_package dir, which can be recognized by python but not pycharm.
I removed them all then reinstalled with pip install and it works after that, luckily the project I was working on came up with a requirements.txt file, so the command for it was:
pip install -r ./requirement.txt
I just ran into this issue in a brand new install/project, but I'm using the Python plugin for IntelliJ IDEA. It's essentially the same as PyCharm but the project settings are a little different. For me, the project was pointing to the right Python virtual environment but not even built-in modules were being recognized.
It turns out the SDK classpath was empty. I added paths for venv/lib/python3.8 and venv/lib/python3.8/site-packages and the issue was resolved. File->Project Structure and under Platform Settings, click SDKs, select your Python SDK, and make sure the class paths are there.
pip install --user discord
above command solves my problem, just use the "--user" flag
I fixed my particular issue by installing directly to the interpreter. Go to settings and hit the "+" below the in-use interpreter then search for the package and install. I believe I'm having the issue in the first place because I didn't set up with my interpreter correctly with my venv (not exactly sure, but this fixed it).
I was having issues with djangorestframework-simplejwt because it was the first package I hadn't installed to this interpreter from previous projects before starting the current one, but should work for any other package that isn't showing as imported. To reiterate though I think this is a workaround that doesn't solve the setup issue causing this.
If you are having issues with the underlying (i.e. pycharm's languge server) mark everything as root and create a new project. See details: https://stackoverflow.com/a/73418320/1601580 this seems to happy to me only when I install packages as in editable mode with pip (i.e. pip install -e . or conda develop). Details: https://stackoverflow.com/a/73418320/1601580
--WINDOWS--
if using Pycharm GUI package installer works fine for installing packages for your virtual environment but you cannot do the same in the terminal,
this is because you did not setup virtual env in your terminal, instead, your terminal uses Power Shell which doesn't use your virtual env
there should be (venv) before you're command line as shown instead of (PS)
if you have (PS), this means your terminal is using Power Shell instead of cmd
to fix this, click on the down arrow and select the command prompt
select command prompt
now you will get (venv) and just type pip install #package name# and the package will be added to your virtual environment
On windows I had to cd into the venv folder and then cd into the scripts folder, then pip install module started to work
cd venv
cd scripts
pip install module
instead of running pip install in the terminal -> local use terminal -> command prompt
see below image
pycharm_command_prompt_image
In your pycharm terminal run pip/pip3 install package_name

Dependency management between multiple Python modules

I have a project with multiple Python modules, each of which has its own virtual environment.
Project Structure:
data-reader (Python module)
data_reader
reader.py
venv (virtual environment directory for data-reader)
requirements.txt
data-writer (Python module)
data_writer
writer.py
venv (virtual environment directory for data-writer)
requirements.txt
commons (Python module)
commons
utils.py
venv (virtual environment directory for commons)
requirements.txt
setup.py
I want to install commons as a dependency in both modules: data-reader and data-writer and preferably as an editable project dependency.
I created setup.py in commons and added the following requirement to the requirements.txt in both modules:
-e commons
When I activate the virtual environment for one of the modules and install its requirements I can run scripts from commons in the Python interpreter using the terminal, which is the expected and desired outcome, but in the Intellij IDEA IDE, I get an error underlining the import statement and with this error message: Unresolved reference 'commons'
from commons import utils
I don't know if it is a problem in IDEA or in the approach I am using.
What is the recommended way to add and manage such dependencies?
Is adding this dependency as an editable project one a good idea in the first place or there is other recommended approaches?
The problem seems to be related with Intellij / Pycharm.
You should be choosing the interpreter used by Pycharm to match the one of the virtualenvironment.
If you don't, Pycharm will be using the default python interpreter where the libraries you are importing are not installed.
You can select it with settings -> preference -> Project Interpreter (in the left menu bar).
Then add the local virtualenv to the specific python project, for the three of them.
If it helps, the official documentation is:
https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html
As it has been a while, I am posting an answer for other readers. The approach turned out to be good for us and working fine so far.
The issue is with IntelliJ/PyCharm and it turned out to be a very old one which is still not properly fixed: https://youtrack.jetbrains.com/issue/PY-976. A couple of workarounds are reported in the issue itself. I personally have not tried all of them but none of what I have tried so far worked for me.
Another thread is available here: PyCharm does not recognize modules installed in development mode

Visual Studio Code pylint: Unable to import 'protorpc'

I'm using pylint in Visual Studio Code to develop a Google App Engine (GAE) Cloud Endpoint API in Python. I'm unable to resolve a lint error. I don't know what's causing the error, but at a guess, pylint cannot find the protorpc library?
The recommended fix in Troubleshooting Linting is to configure workspace settings to point to fully qualified python executable. I have done this, but the lint error remains.
protorpc itself is installed to:
~/google-cloud-sdk/platform/google_appengine/lib/protorpc-1.0/protorpc
...and this contains the remote.py module that cannot be imported:
__init__.py generate_python.py protojson.py transport.py
definition.py google_imports.py protourlencode.py util.py
descriptor.py message_types.py registry.py webapp
generate.py messages.py remote.py wsgi
generate_proto.py protobuf.py static
I've added this path to $PYTHONPATH (along with the kitchen sink):
export GOOGLE_CLOUD_SDK=~/google-cloud-sdk
export APPENGINE_PATH=$GOOGLE_CLOUD_SDK/platform/google_appengine
export PYTHONPATH=$PYTHONPATH:$GOOGLE_CLOUD_SDK
export PYTHONPATH=$PYTHONPATH:$GOOGLE_CLOUD_SDK/lib
export PYTHONPATH=$PYTHONPATH:$GOOGLE_CLOUD_SDK/lib/googlecloudsdk
export PYTHONPATH=$PYTHONPATH:$GOOGLE_CLOUD_SDK/lib/googlecloudsdk/api_lib
export PYTHONPATH=$PYTHONPATH:$GOOGLE_CLOUD_SDK/platform/google_appengine/lib
export PYTHONPATH=$PYTHONPATH:$GOOGLE_CLOUD_SDK/platform/google_appengine/lib/protorpc-1.0/protorpc
The application runs locally and also when deployed, so this appears to be just a lint error, but it's frustrating that I can't solve it.
Using third-party libraries states:
The Python runtime in the standard environment includes the Python
standard library, the App Engine libraries, and a few bundled
third-party packages.
Because of this, I assumed 'the App Engine libraries' includes protorpc, but I'm unsure. Moreover, Adding the Cloud Endpoints Frameworks library to the sample API only requires google-endpoints be installed to the app's lib directory:
pip install -t lib google-endpoints --extra-index-url=https://gapi-pypi.appspot.com/admin/nurpc-dev --ignore-installed
My point is, I don't think I've not installed something, and I don't think I'm missing anything in my (web) app's lib directory.
Changing the library path worked for me. Hitting Ctrl + Shift + P and typing python interpreter and choosing one of the available shown. One was familiar (as pointed to a virtualenv that was working fine earlier) and it worked. Take note of the version of python you are working with, either 2.7 or 3.x and choose accordingly
I was facing same issue (VS Code).Resolved by below method
1) Select Interpreter command from the Command Palette (Ctrl+Shift+P)
2) Search for "Select Interpreter"
3) Select the installed python directory
Ref:- https://code.visualstudio.com/docs/python/environments#_select-an-environment
Open the settings file of your Visual Studio Code (settings.json) and add the library path to the "python.autoComplete.extraPaths" list.
"python.autoComplete.extraPaths": [
"~/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.5.2",
"~/google-cloud-sdk/platform/google_appengine",
"~/google-cloud-sdk/lib",
"~/google-cloud-sdk/platform/google_appengine/lib/endpoints-1.0",
"~/google-cloud-sdk/platform/google_appengine/lib/protorpc-1.0"
],
For your case, add the following code to vscode's settings.json.
"python.linting.pylintArgs": [
"--init-hook='import sys; sys.path.append(\"~/google-cloud-sdk/platform/google_appengine/lib\")'"
]
For the other who got troubles with pip packages, you can go with
"python.linting.pylintArgs": [
"--init-hook='import sys; sys.path.append(\"/usr/local/lib/python3.7/dist-packages\")'"
]
You should replace python3.7 above with your python version.
Spent hours trying to fix the error for importing local modules. Code execution was fine but pylint showed:
Unable to import '<module>'
Finally figured:
First of all, select the correct python path. (In the case of a virtual environment, it will be venv/bin/python). You can do this by hitting
Make sure that your pylint path is the same as the python path you chose in step 1. (You can open VS Code from within the activated venv from terminal so it automatically performs these two steps)
The most important step: Add an empty __init__.py file in the folder that contains your module file. Although python3 does not require this file for importing modules, I think pylint still requires it for linting.
Restart VS Code, the errors should be gone!
I've not played around with all possibilities, but at least I had the impression that this could be a python version related issue. No idea why, I just trusted my gut.
Thus I just changed the pythonPath to python3 (default: python):
"python.pythonPath": "python3"
I reinstalled the dependencies (including pylint!!!) with
pip3 install <package> --user
... and after restarting vs code, everything looked fine.
HTH Kai
I was still getting these errors even after confirming that the correct python and pylint were being used from my virtual env.
Eventually I figured out that in Visual Studio Code I was A) opening my project directory, which is B) where my Python virtual environment was, but I was C) running my main Python program from two levels deeper. Those three things need to be in sync for everything to work.
Here's what I would recommend:
In Visual Studio Code, open the directory containing your main Python program. (This may or may not be the top level of the project directory.)
Select Terminal menu > New Terminal, and create an virtual environment directly inside the same directory.
python3 -m venv env
Install pylint in the virtual environment. If you select any Python file in the sidebar, Visual Studio Code will offer to do this for you. Alternatively, source env/bin/activate then pip install pylint.
In the blue bottom bar of the editor window, choose the Python interpreter env/bin/python. Alternatively, go to Settings and set "Python: Python Path." This sets python.pythonPath in Settings.json.
The simplest solution is to create a .env file in your project root with this content:
PYTHONPATH=.
You don't need __init__.py files. It works even if your code is in src dir, and unit tests in tests subdirs. This helped pylint and pytest to find all the modules.
For more info, see https://code.visualstudio.com/docs/python/environments#_environment-variable-definitions-file
The visual studio default setting should be the same as the interpreter path.
Change VS code default setting: windows: File > Preferences > Settings
{
"python.pythonPath": "C:\\Users\\Anaconda3\\pythonw.exe",
"workbench.startupEditor": "newUntitledFile"
}
Find the right interpreter:
windows: Ctrl+Shift+P->select interpreter:
the path of that interpreter should be same as the version you are working on.
First I will check the python3 path where it lives
And then in the VS Code settings just add that path, for example:
"python.pythonPath": "/usr/local/bin/python3"
I resolved this by adding the protorpc library to the $PYTHONPATH environment variable. Specifically, I pointed to the library installed in my App Engine directory:
export PYTHONPATH=$PYTHONPATH:/Users/jackwootton/google-cloud-sdk/platform/google_appengine/lib/protorpc-1.0
After adding this to ~/.bash_profile, restarting my machine and Visual Studio Code, the import errors went away.
For completeness, I did not modify any Visual Studio Code settings relating to Python. Full ~/.bash_profile file:
export PATH=/Users/jackwootton/protoc3/bin:$PATH
export PYTHONPATH=/Users/jackwootton/google-cloud-sdk/platform/google_appengine
export PYTHONPATH=$PYTHONPATH:/Users/jackwootton/google-cloud-sdk/platform/google_appengine/lib/protorpc-1.0
# The next line updates PATH for the Google Cloud SDK.
if [ -f '/Users/jackwootton/google-cloud-sdk/path.bash.inc' ]; then source '/Users/jackwootton/google-cloud-sdk/path.bash.inc'; fi
# The next line enables shell command completion for gcloud.
if [ -f '/Users/jackwootton/google-cloud-sdk/completion.bash.inc' ]; then source '/Users/jackwootton/google-cloud-sdk/completion.bash.inc'; fi
I find the solutions stated above very useful. Especially the Python's Virtual Environment explanation by jrc.
In my case, I was using Docker and was editing 'local' files (not direcly inside the docker).
So I installed Remote Development extension by Microsoft.
ext install ms-vscode-remote.vscode-remote-extensionpack
More details can be found at https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack
I should say, it was not easy to play around at first.
What worked for me was...
1. starting docker
2. In vscode, Remote-container: Attach to running container
3. Adding folder /code/<path-to-code-folder> from root of the machine to vscode
and then installing python extension + pylint
Another solution could be generating a pylintrc in project location using command: (this command will by default print the content to terminal and not actually create a file, you need to dump output to .pylintrc)
pylint --generate-rcfile
and then update key init-hook= using:
import sys; sys.path.append("your_project_location")
In my case, I tried flake8, bandit, didn't work, eventually I uninstalled the extension called python (pylance) and everything worked perfectly.
I got the same error on my vscode where I had a library installed and the code working when running from the terminal, but for some reason, the vscode pylint was not able to pick the installed package returning the infamous error:
Unable to import 'someLibrary.someModule' pylint(import-error)
The problem might arise due to the multiple Python installations. Basically you have installed a library/package on one, and vscode pylint is installed and running from another installation. For example, on macOS and many Linux distros, there are by default Python2 installed and when you install Python3 this might cause confusion. Also on windows the Chocolatey package manager might cause some mess and you end up with multiple Python installations. To figure it out if you are on a *nix machine (i.e., macOS, GNU/Linux, BSD...), use the which command, and if you are on Windows, use the where command to find the installed Python interpreters. For example, on *nix machines:
which python3
and on Windows
where python
then you may want to uninstall the ones you don't want. and the one you want to use check if the package causing above issue is installed by
python -c "import someLibrary"
if you get an error then you should install it by for example pip:
pip install someLibrary
then on vscode press ⌘⇧P if you are on a mac and CtrlShiftP on other operating systems. Then type-select the >python: Select Interpreter option and select the one you know have the library installed. At this moment vscode might asks you to install pyling again, which you just go on with.
I resolve this error by below step :
1 : first of all write this code in terminal :
...$ which python3
/usr/bin/python3
2 : Then :
"python.pythonPath": "/users/bin/python",
done.
I had same problem for pyodbc , I had two version of python on my Ubuntu (python3.8 and python3.9), problem was: package installed on python3.8 location but my interpreter was for python3.9. i installed python3.8 interpreter in command palette and it fixed.
Other solutions not working for me (multiple workspaces)
Create ~/.vscode/workspace.env with following content
PYTHONPATH=$PYTHONPATH:/Users/jackwootton/protoc3/bin
Go to Workspace Settings: ⌘ / Ctrl⇧P → Workspace Settings.
Add next config line there:
"python.envFile": "/Users/jackwootton/.vscode/workspace.env",
Reload Window.
This solution better than #Jack, because it doesn't modify a global variable for all system, just for your project workspace.
In my case, the packages got installed in the global Python installation path and not in the venv, even though I had the virtual environment activated at the time of installation.
I could see this when I switched to the base environment (by a click on the bottom left status field of the chosen interpreter) and saw that the package could be imported.
I could only solve this by removing the venv and installing it again.
pylint is run in the vscode terminal, and its output transferred to the editor. Therefore the terminal environment settings need to match those in the command line outside vscode. Specifically, in settings.json:
"terminal.integrated.env.linux": {
"PYTHONPATH": "/path/to/library/files/you/want/pylint/to/FINALLY/EXPLETIVE/FIND"
},

PYTHONPATH vs symbolic link

Yesterday, I edited the bin/activate script of my virtualenv so that it sets the PYTHONPATH environment variable to include a development version of some external package. I had to do this because the setup.py of the package uses distutils and does not support the develop command à la setuptools. Setting PYTHONPATH works fine as far as using the Python interpreter in the terminal is concerned.
However, just now I opened the project settings in PyCharm and discovered that PyCharm is unaware of the external package in question - PyCharm lists neither the external package nor its path. Naturally, that's because PyCharm does not (and cannot reliably) parse or source the bin/activate script. I could manually add the path in the PyCharm project settings, but that means I have to repeat myself (once in bin/activate, and again in the PyCharm project settings). That's not DRY and that's bad.
Creating, in site-packages, a symlink that points to the external package is almost perfect. This way, at least the source editor of PyCharm can find the package and so does the Python interpreter in the terminal. However, somehow PyCharm still does not list the package in the project settings and I'm not sure if it's ok to leave it like that.
So how can I add the external package to my virtualenv/project in such a way that…
I don't have to repeat myself; and…
both the Python interpreter and PyCharm would be aware of it?
Even when a package is not using setuptools pip monkeypatches setup.py to force it to use setuptools.
Maybe you can remove that PYTHONPATH hack and pip install -e /path/to/package.
One option is to add path dynamically:
try:
import foo
except ImportError:
sys.path.insert(0. "/path/to/your/package/directory")
import foo
But it is not the best solution because it is very likely that that code will not get into the final version of the application. One more option (and more appropriate imho) is to make simple setup.py file for package and deploy it in virtualenv with develop parameter or by pip with -e parameter:
python setup.py develop
or:
pip install -e /path/to/your/package/directory
http://packages.python.org/distribute/setuptools.html#development-mode
This is an improvement on ndpu's answer that will work regardless of where the real file is.
You can dereference the symlink and then set sys.path before importing local imports.
import os.path
import sys
# Ensure this file is dereferenced if it is a symlink
if __name__ == '__main__' and os.path.islink(__file__):
try:
sys.path.remove(os.path.dirname(__file__))
except ValueError:
pass
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
# local imports go here

How to setup PyDev's python interpreter in eclipse using pip within a virtualenv

I want to use pip to install new packages and they say to install pip in a virtualenv. I did that and installed some packages and use them no problem.
But I have a problem setting up the simple google app engine helloworld app under PyDev using python2.7. I set it up to use C:\Python27\env\Scripts\python.exe. But running it gave an error that it couldn't import pdb.
Running the virtualenv python from the cmd line and calling 'import pdb' gave no errors. And typing pdb echoed: module 'pdb' from 'C:\Python27\lib\pdb.py'. So from the cmd line, it could find pdb in the root lib dir. The debug out from appengine said it was using the virtualenv python. I made sure that I had C:\Python27\lib in the System PYTHONPATH when setting up the PyDev interpreter, but this did not matter. Strange. (I also have C:\Python27\DLLs and C:\Python27\lib\lib-tk).
So I tried using the root python exe as the interpreter and it works fine. No problem with pdb, so my code is correct. But now it can't find the modules I added using pip in the virtualenv.
It seems there are 3 options for setting this up correctly:
Somehow setup the PyDev python interpreter using the virtualenv python that will find the root pdb package. (Don't know how to do this, but it seems like the right answer.)
Or will it work to use the root python and add the virtualenv site-packages to my path. (not sure if this will work).
Don't listen to the pip advice - install pip in the root env and install my modules in the root env.
I hope someone who has gone through this before can give some advice as to the best way to proceed with the least amount of problems later.
I did some research on the google-appengine-python group and they say to go with option 3: Don't listen to the pip advice when using it with google appengine. appengine does not play nice with virtualenv.
Refs:
Appengine with virtualenv
Strange imports (and WARNINGs) in development server

Categories