Add libraries to PYTHONPATH In Pycharm (Windows 10) - python-3.x

When setting up a Pycharm development environment, I am trying to follow the following instruction, which is for Linux environment. However, my system is virtual environment created by Anaconda, running on Windows 10. How to add Pythonpath in Pycharm with respect to this kind of environment?
Add Libraries to PYTHONPATH
When running locally, the tensorflow/models/research/ and slim directories should be appended to PYTHONPATH.
This can be done by running the following from tensorflow/models/research/:
# From tensorflow/models/research/
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
Note: This command needs to run from every new terminal you start.

You may follow the following steps:
Get the Python executable path for Anaconda:
Go to window's "programs" and search for "Anaconda Command line" then right click on the properties and copy the path then go to the cmd terminal and type the path and then type python.exe at the end till you logged in the python interpreter. This may vary from an OS to another but this works for me under windows 7.
In Pycharm: Add a new Python interpreter by following exactly the instructions listed in https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html.
Use the Anaconda python executable path you got in step1 for the new interpreter called "Conda" for example. Set the new project to use "Conda" interpreter instead of the normal default Python interpreter.
Apply the new changes and restart Pycharm. Open the Settings/Preferences dialog box, and click Project Interpreter page to make sure that the project is pointing to "Conda" interpreter.
Hope this will help!

Related

How do I force VS Code to use Python 3.8.10 interpreter instead och 3.10.6?

When I install VS Code extension for Python in Linux Mint it seems like it comes with a Python 3.10.6 interpreter. "Outside" VS Code I can't find that interpreter. When I start Python in terminal I use 3.8.10 and that interpreter can work with all my Python modules/libraries. In VS Code I can't access any of the extra modules.
I've tried to follow all suggested answers in "Invalid python interpreter selected" prompt vscode with no luck (clear interpreters settings, restart, downgrade Python extension). The only accepted interpreter is 3.10.6. I also tried to add the same path that is used in terminal by both writing it my self and manually cklicking me to the right location. VS Code still don't accept
I don't have this issue on Windows computers but on all Linux computers.
To force VS Code to use a specific version of the Python interpreter, you can specify the interpreter path in the VS Code settings. Here's how you can do it:
Open VS Code and go to the "Settings" tab by clicking on the gear
icon on the left-hand side of the window.
In the search bar at the top of the "Settings" tab, type
"python.defaultInterpreterPath". This will show you the setting for
the default Python interpreter path.
Click on the "Edit in settings.json" button to open the
"settings.json" file.
In the "settings.json" file, add the following line to specify the
path to the Python 3.8.10 interpreter:
"python.defaultInterpreterPath": "/path/to/python3.8.10"
Replace "/path/to/python3.8.10" with the actual path to your Python
3.8.10 interpreter.
Save the "settings.json" file.
Restart VS Code for the changes to take effect.

Numpy and pygame not imported as it should [duplicate]

My Flask App server is running but I have three imports that cannot be resolved.
I have tried:
reinstalling the imports individually
reinstalling requirements.txt
I configured VSCode Workspace with an extra path to my project folder (there is now a .vscode file within the root of my project folder where it was not before)
I have updated my venv path settings in VSCode
Here is my file structure:
- > .vscode
- > client *(React front end)*
- > data
- > server *(Python/Flask back end)*
- > app
- > venv
- config.py
- README.md
- requirements.txt *(this contains the 3 unresolved, along with several that are resolving)*
- .env
- .flaskenv
- .gitignore
- requirements.txt
Unfortunately none of these things have resolved my imports issue and my routes are still not working. Any ideas/suggestions?
Open the Command Palette (Ctrl+Shift+P), then select the Python: Select Interpreter. From the list, select the virtual environment in your project folder that starts with .env.
Run Terminal: Create New Integrated Terminal (Ctrl+Shift+` or from the Command Palette), which creates a terminal and automatically activates the virtual environment by running its activation script.
Install sqlalchemy and mongoengine with command pip install. Once installing them successfully, there will intellisense when you import them and no warnings shown.
Besides, the folder .vscode is to store Workspace settings as well as debugging and task configurations.
If you are using a virtual environment, and even after trying pip installing all the necessary libraries, you have to select the python interpreter that exists in the virtual environment folder.
(Ctrl+Shift+P) then search for "Python: Select Interpreter"
Click "Enter interpreter path" followed by "Find.."
Navigate to your project virtual environment folder
Go into "Scripts" folder and then select "python.exe" as the interpreter.
These steps allow you to select the right python interpreter associated with the project's virtual environment.
Open the Command Palette (Ctrl+Shift+P), then select Python: Clear Cache and Reload window.
Voila, all the import errors vanished.
I specified a path to the python interpreter I'm using within the settings.json file contained in the project repo's .vscode folder.
"python.pythonPath": "path-to-interpreter.python.exe"
Thanks to the following resource! https://dev.to/climentea/how-to-solve-pylance-missing-imports-in-vscode-359b
I ran into this error after an upgrade of my local python version (brew -> manual install), even though the specified interpreter was already /usr/local/bin/python3.
CMD + Shift + P and re-selecting the [same] interpreter fixed the error.
In hindsight, I suspect that a restart of VSCode could have also fixed this. 🤷‍♂️
Perhaps VSCode is using the incorrect Python path for this reason. A base interpreter should be used instead of the vscode interpreter, if necessary.
After verifying my Python interpreter was sourced correctly in VS Code, I simply cleaned my env and reinstalled the project locally and the import errors disappeared. I personally had an odd VS Code/Pylance cache and reinstalling the dependencies and modules fixed it for me.
I was getting the very same error you have and none of the solutions presented here worked me.
I work on a windows machine. I use miniconda to manage my virtual environments. And when I'm coding I launch every program from the command prompt (cmd), including Vscode.
Even tough inside Vscode the python evironment was correctly settled I was getting the very same import error that you mention. The interesting part of it was that I wasn't having any problems to run the code, it was working as usuall. But when coding I wasn't able to see the functions related to the libraries with the import error.
The solution:
Activate the correct conda env for the project before launch Vscode from the command prompt.
Why does this happen?
I BELIVE that this happen because when you don't activate any conda env before launch Vscode. The base conda env is loaded as default. That generates the import conflict. (You can chek this out installing those packagues into the environment and running everything just as you have been doing)

How to resolve 'Import "django.contrib" could not be resolved from source' in VS Code?

This error suddenly came up when I created a new Django project. I used pip to install all the packages in the virtual environment.
I had exactly the same problem and there are several solutions out there, but only #Neha's answer has brought me to the point. The problem here is really simple. VS Code uses your "main" Python interpreter, whereas you should use the one in your virtual environment for Django.
In other words, I have my main Python installation here:
c:\users\yourName\appdata\local\programs\python\python39.
And my VS Code has pointed to exactly this interpreter. But my Django was installed in this path:
c:\users\yourName\source\vscode-repos\basic-app\backend-service\venvs\lib\site-packages.
In order to solve it click on the
VS Code bar with Python Interpreter, you will then see a pop-up window. Choose + Enter interpreter path... then Find.... It will open File Explorer for you. Go to your virtual environment folder, where you have your Django installed (in my case it the vscode-repos\basic-app\backend-service\venvs folder), inside of it go to the Scripts folder and pick python.exe, like this. And you are good to go!
Actually all of the answers were right , but what worked for me is:
Type pip show Django in vsc terminal
Go to the path of intallation mentioned there
It will be inside "lib" by default..go back to scripts
Inside the scripts , there will be python .exe app
Choose this as your interpreter (as per above answers)
Select the Python interpreter in which you have created the virtual environment. I had the same error and this solution worked.
Below is a screenshot that you can refer to.
If you are using VScode, click the python version on the bottom left corner and select the venv where you have installed the packages.
For anyone who can't see the virtual environment then click on the python version below right corner of vscode,click on Enter Interpretor Path,click find,chose the folder that you are working,click on virtual environment name ,then click bin and select python version and the warning will be gone.
In my case (on MacOS) the problem was caused by creating venv using default arguments.
I don't have Scripts directory, but I've got bin inside. The python3 -m venv my_venv command was creating symlinks to python and python3 executables which was confusing for the VSCode. After removing and creating again the venv with below command everything started to work
python3 -m venv --upgrade-deps --copies my_venv
Go to the Python(Extentions) below your screen Like this then select interpreter tab will pop up, then select C:\Python39\python.exe Like this.Hope it helps
What worked for me was to install django within my virtualenv. All other settings were correct in Visual Studio Code.

Virtual environment not available for selection in Visual Studio Code

I have a Python project with several dependencies on modules which are installed into a virtual environment in my working directory.
The python import statements are linted with this message:
No name 'storage' in module 'azure'pylint(no-name-in-module)
Unable to import 'azure.storage.filedatalake._models'pylint(import-error)
I cannot select the virtual environment from the command palette because it is not present.
I tried navigating to the directory above the venv directory and executing .\env\Scripts\activate and then executing the file. However i expect a print() statement to return "something" to the Python terminal.
If you cannot find the interpreter you want in the lower left corner, then you can directly edit your workspace configuration file -settings.json, like the following
In fact, probably because your workspace path is not a standard path defined by the system and VSCode cannot detect your interpreter. If some of your virtual environments are in the same folder, you can use the following settings to let VSCode know where to find your interpreter

What causes this error with hello.py in vscode for Windows: "-bash: C:/Python37/python.exe: No such file or directory"?

When I click the play button to run my hello.py program in vscode, I get the following error in the terminal
PF1FEARB:~/tmp$ C:/Python37/python.exe c:/Users/xtn/hello.py
-bash: C:/Python37/python.exe: No such file or directory
Manually running the script in the terminal works
PF1FEARB:~/tmp$ python hello.py
Hello
My python.exe is installed here:
C:\Users\xtn\AppData\Local\bucache\cbdn10897X>where python
C:\Python37\python.exe
Vscode shows the following installed extension
Name: Python
Id: ms-python.python
Description: Linting, Debugging (multi-threaded, remote), Intellisense, Jupyter Notebooks, code formatting, refactoring, unit tests, snippets, and more.
Version: 2020.2.64397
Publisher: Microsoft
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-python.python
How do I find out which PATH vscode uses to look for executables?
Did you configure the Python interpreter correctly within the vscode editor window itself? You'll have to configure certain things within the IDE itself to run Python code through the editor window. Following the guide for setting up Python in vscode (https://code.visualstudio.com/docs/python/python-tutorial), perhaps here are a few steps you missed:
1.
install the Python extension for VS Code from the Visual Studio Marketplace. For additional details on installing extensions, see Extension Marketplace. The Python extension is named Python and it's published by Microsoft.
2.
Select a Python interpreter:
Python is an interpreted language, and in order to run Python code and get Python IntelliSense, you must tell VS Code which interpreter to use.
From within VS Code, select a Python 3 interpreter by opening the Command Palette (⇧⌘P), start typing the Python: Select Interpreter command to search, then select the command. You can also use the Select Python Environment option on the Status Bar if available (it may already show a selected interpreter, too):
The command presents a list of available interpreters that VS Code can find automatically, including virtual environments. If you don't see the desired interpreter, see Configuring Python environments.
Selecting an interpreter sets the python.pythonPath value in your workspace settings to the path of the interpreter. To see the setting, select File > Preferences > Settings (Code > Preferences > Settings on macOS), then select the Workspace Settings tab.
Note: If you select an interpreter without a workspace folder open, VS Code sets python.pythonPath in your user settings instead, which sets the default interpreter for VS Code in general. The user setting makes sure you always have a default interpreter for Python projects. The workspace settings lets you override the user setting.
I was able to apply the same solution in this post. I changed to PowerShell and it works for me now. Command palette -> Terminal: Select Default Shell -> select PowerShell. Restart vscode.

Resources