I'm trying to run TensorFlow on a virtual environment that I've created and which runs Python 3.6.8 using VS Code (I know anaconda is the preferred way to go but I have to use VS Code). However, when I try to import the package into my .py file, it says that the TensorFlow module cannot be found.
My settings.json file has the following data:
{
"python.pythonPath": "C:\\Users\\user\\Desktop\\AI\\venv36\\Scripts\\python"
}
The above was implemented in accordance with this video: https://www.youtube.com/watch?v=Wuuiga0wKdQ. I know that TensorFlow has been installed since when I go to the virtual environment directory and try to import it within the CLI, it imports the module without any issues.
The TensorFlow module is located in the path: C:\Users\user\Desktop\AI\venv36\Lib\site-packages
But when I put the above path in the settings.json file, it still didn't help.
Based on the information you provide, please refer to the following:
To use python in VS Code, please make sure that the 'python' extension has been installed.
If you are using a 'Windows' system, you need to use "python.exe" when setting "python.pythonPath".
Please check whether the python environment displayed in the lower left corner of VS Code is consistent with the python environment used by the VS Code terminal.
( If they use different pythons, please use the shortcut key Ctrl+Shift+` to open a new VS Code terminal, it will automatically enter the python environment selected in the lower left corner of VS Code. )
More reference: Python environments in VS Code.
Related
I am trying to use Pycharm to develop Libreoffice python Macro with in the virtual environment with not a lot of success. You'll find below a few things I have tried with the differnt errors I have had so far. I welcome any answers on the errors below as well as alternate solutions.
first try : set up directly the venv environment
As when you work with Libreoffice, you need to use the embedded python integrator, the first attempt is when defining the python integrator in pycharm, I reference this embedded interpreter (located somewhere in Program Files\Libreoffice\program` on my windows machine, I click the new venv box in pycharm and get the following error :
Error: standard python 'venv' module not found
So it seems venv is not packaged with the Libreoffice embedded python environment. Is there a way to add it?
Second try : inject embedded environment inside a precreated venv
What I did here was to create the Venv in pycharm using a standard python version aligned with the one libreoffice uses (here 3.8.10). Once the venv is succesfully created, I manually change the path to the python based interpretor to the proper libreoffice one.
I also update the pythonpath in pycharm for this venv to include Files\Libreoffice\program\ so that we can find some preinstalled modules suitable for Libreoffice, in particular uno.py.
This looks a little better, I can launch a libreoffice instance, but when I actually want to do stuff inside this instance from python, I get the following error loading component library </mergedlo.dll> failed
This mergedlo.dll file is in a directory referenced in the python path for this venv.
What configuration did I miss here?
I am using a virtual environment for my python code. I installed the gmsh module inside the folder project\venv\lib\site-packages, and apparently, it is done properly since when I create the "requirement.txt" it appears within the required modules. However, when I open the project folder in Visual Studio Code, and I try to import the gmsh module, it is not found.
Can you give me some hints??
-- Edit
installation
vs code
As this answer suggest, you need to set your Python interpreter in VS Code.
You should select project\venv\bin\python as your interpreter
My basic issue is that I get an error whenever I try to import a Python module in VSCode, which doesn't happen when using PyCharm.
From what I've read so far the problem seems to be with the python path, or the interpreter.
My issue is, that I don't really understand what the issue is, meaning I quickly feel lost when trying to apply some of the other solutions I have found, because I don't know what to look for.
Can someone clarify what is going wrong and how to fix it?
Edit: I probably forgot to add a crucial detail. I was facing this issue when running my python files using the code runner.
This is what I needed to add to my settings.json in order to get everything working completely:
"code-runner.executorMap": {
"python": "$enterPythonPathhere"
}
In VS Code press Ctrl+Shift+P
Type select interpreter in the search box
Python: Select Interpreter
This will show a couple of environment paths. Click on the environment you were using in pycharm.
If your pycharm environment does show on the list you should enter the environment path instead. That should work.
For installing and using python modules in VS Code, please refer to the following:
As people mentioned, select the desired python environment in the lower left corner of VS Code.
Please use the shortcut key Ctrl+Shift+` to open a new VS Code terminal, and then check whether it has entered the selected python environment.("python --version" or "pip --version")
Install the module.
Check the installation of the module.(pip show module_name)
Reference: Python environments in VS Code.
I installed python virtual env. I use vs code. I imported scrapy in my code and vs code doesn't recognize the module. Actually, it works well when I run it. scrapy crawl tester
Just, vs code shows red underlines that mean "Unable to import scrapy" So this is just vs code issue, not venv or scrapy package install issue.
This code works well and actuallay, scrapy is imported without any issue. This is just vs code issue. Thank you.
You might be able to solve your problem by using CTRL+Shift+P to add "Python: Select Interpreter" to your project.
If you created a virtual environment and activated it as well
type which python on macOS/Linux,
type where python on Windows,
inside the activated terminal session.
(env) userpc#pc:~$ which python
/home/userpc/Desktop/foldername/env/bin/python
In VSCode press Ctrl+Shift+P, under >Select Interpreter paste the location you receive using the former command.
Once done, restart VSCode.
VSCode will also ask you to install pylint in your environment and if it doesn't you can do the same by activating you environment and typing
pip install pylint
inside the activated terminal session.
Ran into the same problem - selected the correct Python interpreter in VS Code, pip installed all the desired Python libraries but import is still underlined in VS Code.
What you need to do
What worked for me is to make sure that the linter that you are using in this VS Code instance (I was using pylint) is from the bin folder of the virtual environment, not somewhere else.
How you can do it
I'll use the absolute path to the desired virtual environment /User/ProjectFolder/env as an example.
To check that you meet the conditions stated in What you need to do, toggle the settings.json file in VS Code by pressing ⌘ + ,:
Make sure that the correct Python interpreter is selected. i.e. "python.pythonPath":/User/ProjectFolder/env/bin/python3.
Make sure that the linter (e.g. pylint) is located in that bin folder, not anywhere else. i.e "python.linting.pylintPath":/User/ProjectFolder/env/bin/pylint, NOT something like "python.linting.pylintPath":/usr/local/bin/pylint.
This means that you have to install your desired linter in the virtual environment.
Hope this helps.
I tried #cleon-w's answer which worked for me. (Thanks Cleon)
I was using pyenv on my Mac M1 (Big Sur) with vscode. vscode could not resolve the
paths to python that the pyenv provides, so I looked within the pyenv scripts to find the absolute paths
to the underlying python and the pylint files.
As #Cleon W says I ensured pylint and python are in the same bin directory.
Then edit VS Code settings.json to point to them directly (bypass .pyenv) and the imports could be found.
settings.json (vscode)
"python.defaultInterpreterPath": "/Users/USERNAME/.pyenv/shims/python",
"python.pythonPath": "/Users/USERNAME/.pyenv/versions/3.9.1/bin/python",
I have similar problem with Django. What solved my problem was:
create .vscode/settings.json file inside root dir for project.
add this json {"python.defaultInterpreterPath": "path to bin directory in root dir for project"}
Ctrl-Shif-P -> Python: Select Interpreter -> select one that said: Use Python from python.defaultInterpreterPath. If it's not in the list, hit refresh (circled refresh button on the top of settings box).
I tried to open a python script with VS code. The library that requires with the script is in a virtualenv.
I am not sure how to integrate this with VS Code. I could not find my virtualenv in the intepreter.
For your information, the virtualenv I created is called "vc-web" and the scripts is in the folder called "web_scraper" inside the "vc-web" folder.
If your project is in folder e.g. proj, and you have a virtual environment at proj/vs-web and it's the only virtual environment in that directory, then it should be listed by the Python: Select Interpreter command where you can select it.
All you need to do is just specify the "python.pythonPath" as follows. The values for "python.autoComplete.extraPaths" will be determine during runtime, however you are still free to specify custom paths in there.
Please remember to re-start VS Code once the necessary changes have been made.
{
"editor.rulers": [80,100],
"python.pythonPath":"~/path/to/venvs/proj/bin/python"
}