In my workingdirectory I have many folders with a python script modifying data in the same folder.
When running Python in VsCode I need to give a relative path from the working directory into the folder. For example using os.getcwd(), test.py is in D:\Workingdirectory\Folder1: VsCode says, D:\Workingdirectory. Running it in console: D:\Workingdirectory\Folder1.
Direct path is not an option.
How fo i fix it?
Thanks in advance!
This is caused by vscode using workspace as root floder.
This will lead to a problem. When you use the os.getcwd() method in the deep directory of the workspace, you will still get the workspace directory.
You can open your settings and search Python > Terminal: Execute In File Dir then check it.
You can also use debug mode and add the following to your launch.json:
"cwd": "${fileDirname}"
Related
I am running this notebook in my managed notebooks environment on Google Cloud and I'm getting the following error when trying to install the packages: "WARNING: The script google-oauthlib-tool is installed in '/home/jupyter/.local/bin' which is not on PATH.
Consider adding this directory to PATH."
Here is the python code that I'm trying to run for reference. https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/model_monitoring/model_monitoring.ipynb
Any suggestions on how I can update the package installation so it is on path and resolve the error? I'm currently working on GCP user-managed notebooks on a Mac.
Thanks so much for any tips!
RE
Open up your shell config file (likely .zshrc because the default shell on Mac is now zsh and that's the name of the zsh config file) located at your home directory in a text editor (TextEdit, etc) and add the path to the executable.
Like this:
Open the file:
open -e ~/.zshrc
Edit the file:
Add this line at the top (may vary, check the documentation):
export PATH="/home/jupyter/.local/bin"
That may not work, try this:
export PATH="$PATH:/home/jupyter/.local/bin"
Your best bet is to read the package documentation.
After saving the config file, run source ~/.zshrc and replace .zshrc with the config file name if it's different OR open a new terminal tab.
What this does is tells the shell that the command exists and where to find it.
I'm trying to write a python library with the following structre on vsc. When I run tests I got an error no module named pytrader. pytrader is a python package it has an init on it. In pycharm i used to set the src folder as the source directory and everything works. Any help thank you
enter image description here
The action of set the src folder as the source directory in the PyCharm is to add the src folder path to the PYTHONPATH.
In the VSCode, it has no way to add it directly, but it's possible to edit it when you debugging, you can add this in the launch.json file:
"env": {"PYTHONPATH":"${workspaceRoot}/src"},
And you can add it directly in the python file like this:
sys.path.append("D:\\TestPlace")
I'm trying to add the src folder to my PYTHONPATH i.E. do the same thing as Mark directory as sources root in PyCharm.
I've looked at the solutions to Visual Studio Code - How to add multiple paths to python path? but the solutions shown there do not work for me.
I've set up my minimal example as follows shown in the screenshot below:
My understanding would be that having a .env file with PYTHONPATH="./src;${PYTHONPATH}" would add the src file to the path.
Nevertheless, when running the code runner or when running python change_pyhton_path.py src is not part of the PYTHONPATH and the direct import from src fails.
I do have setting "python.envFile": "${workspaceFolder}/.env".
In pyCharm on the other hand everything works as it should after hitting Mark directory as source on src.
Thanks for helping out!
in your settings.json add:
"terminal.integrated.env.windows": {
"PYTHONPATH": "full python path here"
}
if you have problems with importing modules and you are using code-runner, try to add
"code-runner.fileDirectoryAsCwd": true
to your settings.json file
If you are trying to get auto complete working from your source directory, you could add to the PYTHONPATH environment variable as you are doing. You can also go the "vscode native" route, as there is a configuration. Open your workspace settings and add the following line:
"python.autoComplete.extraPaths": ["./src"]
NOTE: Avoid setting this at the user level as each project differs in where the source code lives
I dont know much but the json PYTHONPATH setting above looks like it would work as the json.settings file is definatly going to nead to know about your interpreter. Once you have a Python terminal prompt you have to run pythons pip file to integrate all the neccesary libraries . VS Code itself tells you to go to the command pallete and install either a venv virtual environment or create a conda (from anaconda or miniconda) set up for it. I am trying to do it myself so once I crack it I'll have more info.
I am trying to run python code using the jupyter extension in vscode here
I have a workspace open at this location
/Users/user/Documents/
When I try and run the following code in a file called test.py in a child directory, the current working directory is set at the workspace level rather than the file. Is it possible to change a setting to use the cwd of the file, rather than the workspace? I cannot find one in settings.json and the "cwd" in launch.json only seems applicable to debugging.
File location:
/Users/user/Documents/python_code/test.py
#%%
import os
print(os.getcwd())
Expected output:
/Users/user/Documents/python_code/
Actual output:
/Users/user/Documents/
When running the same code through the terminal it prints the expected result, so the issue seems to be related to the jupyter extension
For any specific folder / workspace that you have open in VS Code you can use the notebookFileRoot setting to set a specific absolute path directory to always set the Jupyter working directory to when you start the Interactive Window with that folder open.
Always opening on the file location (without having to set notebookFileRoot to an absolute path per folder) is not supported via the notebookFileRoot setting. The VSCode variables such as ${fileDirname} are specific to task and debug configuration files (launch.json and task.json). We've specifically added code to recognize ${workspaceFolder} for our settings page but we don't recognize other VSCode variables in there.
If you want there is a github item here that has suggested this feature. You can follow that or vote it up if you want this feature added.
https://github.com/Microsoft/vscode-python/issues/4441
Edit 11/5/2019:
We have now made a change to allow the VS Code "Notebook File Root" setting to be set to ${fileDirname}. This setting will now start up Notebook Editor and Interactive Window session relative to the file location that was used to start them.
There is the setting python.dataScience.notebookFileRoot which is, as far as I understand, supposed to achieve the expected behaviour when setting it to ${fileDirname}.
See correspoding source.
However, it does not seem to work in my case. Maybe a bug?
Note that the output of running the script from the terminal depends on you working directory of the terminal!
I just downloaded and installed Anaconda on my Windows computer. However, I am having trouble executing .py files using the command prompt. How can I get my computer to understand that the python.exe application is in the Anaconda folder so it can execute my .py files?
You should use Anaconda Prompt instead of common Windows command prompt.
Then navigate to your folder with the .py file and run:
python myfile.py
However if you want to use normal command prompt you should put the path with you're python.exe which is usually in
C:\Users\<username>\AppData\Local\Continuum\anaconda3\python.exe
behind this one put your .py file.
Launch JupyterLab from Anaconda
(Perform the following operation with JupyterLab ...)
Click on icon folder in side menu
Start up "Text File"
Rename untitle.txt to untitle.py (The name of the file started up was also changed)
Start up the "terminal" (In windows the power shell starts up)
Execute the command python untitle.py
Right click on a .py file and choose 'open with'
Scroll down through the list of applications and click something like 'use a different program'
Naviage to C:\Users\<username>\AppData\Local\Continuum\anaconda3
click on python.exe and then click on 'ok' or 'open'
Now when you double click on any .py file it will run it through Anaconda's interpreter and therefore run the python code.
I presume if you run it through the command line the same would apply but perhaps someone could correct me?
Just get to the home of jupyter notebook and select "New" then select "Text file".
Then save the text file as file_name.py
Write your code in the file and save the file.
Then open the "Anaconda Prompt" and then type as follows to run your file
python file_name.py
You can do it from the "Anaconda Prompt"
conda run "my_script.py"
I was doing exactly as Martin Bosch suggested, and was getting the following:
(base) C:\>python command.py
python: can't open file 'command.py': [Errno 2] No such file or directory
I solved it this way:
navigate to the exact file location using the "cd" command
for me this was:
(base) C:\>cd my_scripts
this should put you specifically in the file where your .py script is located.
now you should try to input the name of your file.
(base) C:\my_scripts> test_script.py
you may get asked which program to run this with, and simply find python.exe
After doing this process once, I can simply type (in anaconda prompt)
test_script.py
and it runs no problem, even from the top of the file tree (I don't have to be in the exact file, nor do I have to explicitly give the whole file path)
Anaconda should add itself to the PATH variable so you can start any .py file with "python yourpythonfile.py" and it should work from any folder.
Alternatively download pycharm community edition, open your python file there and run it. Make sure to have python.exe added as interpreter in the settings.
If you get the following error:
can't open file 'command.py': [Errno 2] No such file or directory
Then follow this steps to fix it:
Check that you are in the correct directory where the Python file is.
If you are not in the correct directory, then change the current working directory with cd path. For instance: cd F:\COURSE\Files.
Now that you are in the directory where your .py file is, run it with the command python app.py.
Check where is the directory for the ananconda environment directory which is generally
"C:\Users\[UserName]\.conda\envs\[conda environment directory]"
You will see python.exe in that directory.
After that, you need to use the following command to execute your python file (i.e. xx.py) when you are running Anaconda prompt and you will be done:
"C:\Users\[UserName]\.conda\envs\[conda environment directory]\python.exe" xxx.py
BTW, if you have global variable (i.g. variable yyy) that contain directory, you have to define the global variable that contains full path of directory just below the header (the import section) to prevent the "name 'yyy' is not defined" error to occur:
from pathlib import Path # dealing with path issue
yyy = Path("[DriverLettter]:\Full\Path\of\Directory")