VSCode python not finding relitive files in Windows Subsystem for Linux - python

I'm following the Practical Python Programming course from David Beazley. Having recently switched machines (Ubuntu to Windows 10...i know...) I'm picking this back up.
Environment: Visual Studio Code, using Windows Subsystem for Linux (WSL) running Ubuntu, all the necessary VSCode extentions applied to work with WSL.
Context: python allows you to reference files with code like the below (adapted from the course linked to above). In this case my python module is in a parent directory called Work, and within it there is a directory called Data that has various files in it.
read_data('Data/portfolio.csv', 'Data/prices.csv')
With WSL, you can launch VSCode by doing code . and hitting enter in a Windows Terminal once you are in the directory with a file you want to edit. In my case, doing so in my Work directory (on Ubuntu WSL) starts VSCode and I can run a python file, and as I would expect, I can find the files in the Data/ directory (to be clear, the read_data(... function works).
Issue: if I come into WSL via the Windows GIU first (i.e. clicking the shortcut on my Windows 10 desktop), navigate to the folder on WSL with my code, and run the same file...I get python errors to say it can't find the files I'm referencing. My fix is to reference the full file path.
Why would this be? I've noticed if I do a print Path.cwd() using pathlib while running the file as described above in "Issue" then the working directory is my Ubuntu home, not the directory where the code is.
Is there an interpreter setting or config in VSCode I'm missing?

Related

Why can't I run python in git bash and visual studio code?

I'm having a hard time understanding and running a terminal. I downloaded python 3.11 from the official website and installed it. I also checked the box at the beginning of the installment to add python to PATH. But when I try to run it on git bash it just gives me an empty line. On Visual code, It takes to download python through the Microsoft store. But is it makes sense to download it twice, through the website and through the store...?
git bash picture:
visual code terminal picture:
On Visual code, It takes to download python through the Microsoft store
If my understanding is correct, Windows comes with dummy python executables python.exe and python3.exe that take you to the microsoft store to install it- after which it is a real python executable instead of a dummy one. If you run where python, you'll get a list of all the python executables that are found via the PATH variable in the order that they are found in the PATH, where one of them will be the Windows one (instead of the one you installed from the Python website). Likely, the one you'll see listed first will be C:\Users\you\AppDate\Local\Microsoft\WindowsApps\python.exe.
What you probably need to do is update the order of entries in your PATH environment variable so that the directory containing the one you installed from the Python website is earlier in the PATH environment variable than the directory of the out-of-box Windows one.
Note: As indicated in this question: why must python executable path be added to system PATH environment variables for python command to work in visual studio code, you might need to add the directory containing your from-website Python to the system PATH environment variable (before/above the path to the directory of the system one) (as opposed to the user PATH environment variable).

Can I run the same Python code in Pycharm for mac & windows?

I have just started using python and pycharm, and I have a windows laptop and a mac where I need to work from. In specific, I created a project including a venv in my windows laptop which is uploaded in one drive and works perfectly for that laptop. So my question is; can I work on that same project from my mac?
Instinctively my answer would be no, because all the virtual environment files originally created in the windows venv are .exe which are incompatible with anything mac related. Trying to open the project and load the interpreter myself has confirmed that this doesn't work.
But then I stumbled on to this post, which would suggest that this person can run a project from a windows and linux OS with the only problem being that they only have to re-select the interpreter.
So is there any way that the above can be done, or a workaround at least? (e.g. creating two venvs for each OS or something).
Yes, you can run the same Python code on both Windows and Mac. You will need to create separate virtual environments on each because the Python interpreter is a .exe file on Windows and something else on Mac.
I suggest that you use git to track changes in your code. Then you can push your code to a service such as GitHub so that your changes are avaialble on all of your computers. You won't add the virtual environments to the git repository, though. These should be created separately on each machine where you want to work on your project.

Pycharm debugger: breakpoint links to remote_source files

While using the pycharm debugger breakpoints are linking to copies of files saved in remote_sources,
C:\Users\me\.PyCharm2019.2\system\remote_sources\921495913\41753927\pipeline.py
rather than reading the correct project file. C:\Users\me\project\pipeline.py.
The problem occurred after I had updated some python packages (pandas, pytest). Not sure if it is related.
I have my root set to C:\Users\me\project\ and WSL mapping C:\Users\me\project\ to /mnt/c/Users/me/project. These have not changed and have been working fine in the past.
I am using Pycharm professional 2019.2, Windows 10 running Ubuntu with WSL and have WSL remote interpreter configured in Pycharm.
How can I remove the link to \.PyCharm2019.2\system\remote_sources\ and debug on local files as I always have in the past?

'Cannot setup a Python SDK' in PyCharm project using virtualenv after OS reinstallation

I re-installed windows and opened an existing Pycharm project and get the error 'SDK seems invalid' in Settings > Project Interpreter.
The project interpreter path is pointing to python in the venv:
MyProject\venv\Scripts\python.exe
I tried re-adding python.exe:
Thats when I get the error:
Update: here is an error from idea.log, a lot of other issues for virtual environments seem to be with windows environment variables and system paths:
2018-09-28 19:50:40,275 [ 17601] INFO - hon.packaging.PyPIPackageCache - Loaded 153296 packages from C:\Users\Matt\.PyCharm2018.2\system\python_packages\pypi-cache.json
2018-09-28 19:50:40,816 [ 18142] INFO - rains.python.sdk.PythonSdkType - Fatal Python error: Py_Initialize: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00000fa8 (most recent call first):
Exit code -1073740791
2018-09-28 19:50:40,816 [ 18142] ERROR - ns.python.sdk.PythonSdkUpdater - Failed to determine Python's sys.path value:
STDOUT:
STDERR: Fatal Python error: Py_Initialize: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00000fa8 (most recent call first):
com.jetbrains.python.sdk.InvalidSdkException: Failed to determine Python's sys.path value:
STDOUT:
STDERR: Fatal Python error: Py_Initialize: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'
What solution is: Check out venv\pyvenv.cfg and provide a valid path to the basic python installation.
What has most probably happend:
After reinstalling your OS, you have no base python interpreter reinstalled or you have installed it at different location than before. Thus your virtual environment fails to locate the python installation. Virtual environment implies that all libraries and settings are isolated from other projects. It does not provide an isolated python installation. You still need your base python that had been used for venv creation.
My case: I have a project in a network share and try accessing it from different computers. The base python paths depend on the very PC. The solution above works fine for me. Unfortunatelly, I need to update pyvenv.cfg depending on the PC in use.
P.S. I believe that there is an environment variable to overide the venv config value. I only tried to set PYTHONPATH=C:\Anaconda3\envs\python37 on Windows and then to activate venv. It did no effect and I gave it up.
Here's what solved my problem when I faced the exact same issue
Navigate to Project Interpreter, right side of the selection box, click the gear icon, it will show two options add & show all.
Click show all, if you see the previous existence from the same directory delete that. Click on add or + sign to add a new interpreter and navigate to your project path, navigate down to the virtual environment directory.
venv/bin/{select the python executable with the version code i.e if you are using python3.6 select python3.6}
Once selected, you can now click the notice that says install packaging tools, hit apply and done.
Most probably, some path to Python environment that PyCharm tries to use has become invalid somewhere. There are (at least) two primary suspects:
Path to your virtualenv in PyCharm settings
PyCharm needs to know the path to your environment to run things in it. So, if that path changed, PyCharm's saved path has become invalid.
Go to the interpreter settings for your project in File->Settings...->Project interpreter->(Gear icon)->Show all...:
Then delete and recreate the necessary entries. Or edit them and specify correct paths.
E.g. this is what my list looks like after I deleted an Anaconda installation:
Path in the virtualenv to its base installation
Since virtualenv is not a full installation, it must have a path to its base installation stored somewhere to be able to use files from there.
As of this writing, virtualenv (v16.0.0) in Windows is implemented like this:
The real python.exe and several other files are copied into the virtualenv's subtree
In Lib\orig-prefix.txt, the path to the base installation is stored. It is used to add the base installation's Lib to sys.path via a custom site.py.
So, if the path in that file becomes invalid, the virtualenv's Python interpreter will be unable to find any standard modules except those few that were copied. Which perfectly fits your symptoms.
I currently have the same issue, that I can't install any package tool in Pycharm and that freak me out. Here is the step I fixed it, just want to share. OuO.
Step 1
Find show all in the right corner of a setting icon
Step 2
Click the + icon to open Add python Interpreter
Step 3
In New environment 's Location Under Virtualenv Environment select file
Step 4
Find your Pycharm file and make an empty file under it and click OK and keep click OK
Step 5
Now in Project Interpreter select the empty file you just create and now you should be fine to install Pycharm package.Hope this solve your problem.
In my case the problem was because I was using WSL to host my project so the project address used by PyCharm to create and select the venv was wrong. Instead of using the option Virtualenv Environment to create the environment:
You need to select the WSL option from the side bar and then enter the path to your new or existing venv.
Nothing above worked for me
I made a simple change , Hope it works for you too !!
It happens due to conflict in storing python.exe , In my case it was in F:\ drive
Solution :- Pycharm expects python.exe file to be present in some location , you can check that in project interpreter , it will show some default location where pycharm is searching to execute exe file , but issue is the exe file is not present at that location , so create the folder which pycharm was expecting to execute exe file and paste the downloaded exe file
Hope it Works for you !!
Happy Coding
Well, i'm pritty new to Python, and I did too had a re-install of my os after a crash
Old setup:
In the old system setup I used python 3.7.4. I made al my (practice)projects with that, and each project had a venv/scripts/python3.7.exe in it.... my undertanding is/was that all the files in these virtual environment where 'stand alone', so sufficient to run a python 3.7.4 for that specific projec files, and not depending on files of the home-python-dir.
The install-directory of python was c:\Program Files (x86)\python 37-32
Each project had a file [project-path]/venv/pyvenv.cfg, and in this file there was the line
home = C:\Program Files (x86)\Python37-32
then - my system crashed - reinstalled windows 10, and downloaded again python, but this time python 3.8, and it installed in C:\Program Files (x86)\Python38-32
New setup:
So, after some hairs lost - I also installed the old python version 3.7.4 in the specific path stated in the pyenv.cfg file (C:\Program Files (x86)\Python37-32). So, i had 2 python versions installed on my new system, one in .../python38-32 and one in .../python37-32
And that worked, so when I selected in 'add interpreter/existing interpreter' and pointed to the [project]/venv/python3.7.exe it worked like a charm.
So, apearantly there are files in each version-specific home-directory that pycharm/python needs - i was under the impression that the files in the .venv directory would be all it needed....
I faced this issue when I switched my system which had different python version installed at different location. The simple short solution is to open 'pyenv' file and point it to the current installation path in your current system and that's it.
I had the same problem and couldn't really figure it out.
As it was a side project the mistake was infuriating and hilarious at the same time.
My folder names had non-english letters. Specifically it had the letter "đ" and the SDK was always invalid because of it.
You might say a stupid mistake, it was, but I wasn't really paying attention.
Hope this helps somebody.
I get the issue in Pycharm 2021.2.3 when I try to make a new environment using an exe that is called anything but python.exe (I was organising my different versions by calling them python39.exe, python38.exe, etc.). I just stopped doing that and renamed all my python exes to python.exe and it started working.
go to the Edit Configuration foe edit interpreter, then remove all interpreter then ok.
now, you should add new interpreter and select the path of python.exe in your installation path(for example c:\ ),then ok.
for me, i solved this error by this solution.
The paths specified in pyvenv.cfg need to be corrected after the venv folder is copied from one computer to another. The main place to look at is the user name. For example:
Old computer:C:\Users\OldComputerUserName\AppData\Local\Programs\Python\Python310
New computer:C:\Users\NewComputerUserName\AppData\Local\Programs\Python\Python310
The path is fixed when you add interpreter to the project the 1st time. I learned it the hard way too.
This problem is because the interpreter path points to the virtual-env, instantiated to the particular project. But we don't have any python installed there.
Therefore we have to set the interpreter path to the "python.exe" file inside the python folder which we have installed on our PC.
This can solve this problem very easily.
If you haven't installed python on your machine please install it and set the path accordingly.
Thank you.
I also had this issue and it's 2023! All I can tell you is what I did to fix it. I tried the other solutions listed in this post (at least the ones I looked at) and they didn't work.
What I did was I copied the venv file in my project (for safekeeping) and then moved it elsewhere.
I deleted the venv file in my project after making my backup and I deleted the interpreter that I had been trying to use.
From there I opened up File -> Settings -> Python Interpreter -> and since there was now no interpreter and no venv folder
I created the interpreter again. Make sure to list the proper path to the version of the python exe you want to use!
From there you pretty much just apply your changes and you will see it pop up with some prompts. It will create a new venv folder for you, just accept the prompts and it worked for me.

How can I install Eclipse Indigo on Windows 7

I've downloaded Eclipse Indigo in preparation of using it to write apps in Python. There doesn’t seem to be an install file. When I double-clicked eclipse.exe the program just ran. Can I move the Eclipse directory anywhere I want it, create a shortcut and place it on my taskbar, and start writing programs?
I put the Eclipse directory in my Path statement, and tried to run it from the command line. It won’t run anywhere but in the Eclipse directory. I already have Python 3.2 installed and it does run anywhere from the command line. Will Eclipse automatically find my Python executable or do I have to do something else?
The instructions are pretty clear ... and pretty simple:
Install your JDK on Win7 (I'd choose the 64-bit JVM for 64-bit Windows, but 32-bit will work on 64-bit Windows equally well)
Unpack the "eclipse" folder from the .zip file anywhere you want. Mine happens to be in c:\eclipse.
Manually create a desktop and/or start menu link to eclipse/eclipse.exe
Here's a great reference on "PyDev":
http://www.vogella.com/articles/Python/article.html
I'd also encourage you to install the latest/greatest Eclipse ("Juno") instead of Indigo.

Categories