[tpg_get_posts post_type="post" numberposts=3 ]
*** these notes are a work-in-progress and will be updated as needed ***
This post documents how I set up projects in Python and how to configure VS Code for debugging.
Project Structure
There are 2 basic forms of project structure that seem to work for me.
Simple Projects
project-name
doc/
data/
inc/
__init__.py
shared-code1.py
shared-code2.py
tests/
__init__.py
test_test1.py
test_test2.py
program.py
program2.py
Complex Projects
This is my preferred setup.
project-name
doc/
data/
tests/
testfiles/
temp&permanent-test-files
__init__.py
test_test1.py
test_test2.py
project-name
inc/
__init__.py
shared-code1.py
shared-code2.py
__init__.py
__main__.py
program.py
Testing
The __init__.py in the testing folder should contain the following code. This adds the project folder to the python path so the imports work as expected.
import os
import sys
PROJECT_PATH = os.getcwd()
PROJECT_FOLDER = os.path.basename(PROJECT_PATH)
SOURCE_PATH = os.path.join(
PROJECT_PATH,PROJECT_FOLDER
)
sys.path.append(SOURCE_PATH)
VS Code Configuration
The challenge with both of these structures is importing the programs and shared programs in the tests modules. If you are in the main directory, then running the tests with discovery (ie python -m unittest) will generally work. However, if you are in the test directory and trying to debug a test with VS Code, the imports fail with module not found.
The following options in the project.code-workspace will set the PYTHONPATH to the workspace folder so all imports can be found. The PYTHONPATH must be updated in two locations: (1)settings and (2) launch.
Note: The /project-name is only needed for the complex project setup.
{
"folders": [
{
"path": "."
}
],
"settings": {
"python.linting.cwd": "${workspace}/project-name",
"python.analysis.extraPaths": [
"${workspaceFolder}/project-name"
],
"terminal.integrated.cwd": "${workspaceFolder}",
"terminal.integrated.env.windows": {
"PYTHONPATH": "${workspaceFolderFolder}/project-name",
},
"terminal.integrated.env.linux": {
"PYTHONPATH": "${workspaceFolderFolder}/project-name",
},
},
"launch": {
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"env": {"PYTHONPATH": "${workspaceFolder}/project-name"},
"console": "integratedTerminal",
"justMyCode": true
}
]
}
}
WSL
With Windows and WSL, I have a .bashrc modification which opens the bash terminal in my data directory when the terminal is opened from the command line. The code snippet below ignores the change directory command when a terminal is opened from VS Code.
# start in data directory if not vs code
if [ "$TERM_PROGRAM" != "vscode" ]; then
cd /mnt/c/Data
fi
This post is a work-in-progress. Please use at your own risk. If you have suggestions, email them to the webmaster at this site.
Using git as a source code manager, whenever I need to update a plugin, it is a challenge as I never remember the svn commands. And when I upgraded my computer, I opted not to install svn with the idea of just using git and git svn command.
Update the Readme on the Stable Tag
Before starting get the earliest revision number for your plugin. This can be found on the plugin page, under the Development tab. There is a link to the Development Log which will show all the revisions. Note the initial revision number.
<plugin name> => replace with the plugin name, do not wrap the plugin name in <>
-r xxxxx => this is the revision number, replace xxxx with the number from above.
Create the local git repo from the wordpress svn repo. The following command will create a new directory with the name of the plugin:
git svn clone --stdlayout -r xxxx https://plugins.svn.wordpress.org/<plugin name>
Change directories to the local git repo
cd <plugin name>
Confirm the git config has the svn-remote paths to trunk, branches, tags:
git config -l
git branch -r
(shows origin/trunk)
Now the long part. Fetch all the history from the svn repository. The log-window-size options sets the read buffer. The default is 100 and 1000 is faster for my configuration.
git svn fetch --log-window-size=1000
git branch -r
(shows trunk, branches, tags)
git checkout origin/tags/<stable tag>
(headless)
Make your changes
git commit -am"update tested up to value => 6.0"
git svn dcommit
(push to svn stable tag)
Now the trunk & master are out of sync with the tag. Create a git temp-tag branch, merge it to the master and push the git master branch to the svn trunk. Then delete the temp-tag branch
git switch -c <git temp branch name>
git checkout master
Sync the master with the updated stable tag
git merge temp-tag
Sync the trunk with the stable tag
git checkout origin/trunk
git svn dcommit
Now clean up the leftover temp tag branch in git. If you need the current trunk or tag, just do another git checkout origin/trunk or /tags/xxxx
git branch -d temp-tag
in Python run import tkinter;tkinter._test()
_tkinter.TclError: Can't find a usable init.tcl in the following directories:
_tkinter.TclError: Can't find a usable tk.tcl in the following directories:
Searching on the errors, there were several references environment variables TK_LIBRARY and TCL_LIBRARY,
set TCL_LIBRARY=C:\Python3\tcl\tcl8.6
set TK_LIBRARY=C:\Python3\tcl\tk8.6
The solution most often found was to copy the contents of the Python3/tcl/ folder to the Python3/Lib folder, but that seems to pollute the Lib folder.
My solution is to create symbolic links in the Lib folder:
as administrator open a console window:
mklink /J c:\Python3\Lib\tcl8.6 c:\Python3\tcl\tcl8.6
mklink /J c:\Python3\Lib\tk8.6 c:\Python3\tcl\tk8.6
So far, this has worked. There are many other folders in the TCL directory, so additional links may be needed.
[tpg_get_posts post_type="tpg_news,tpg_products" ]