Get current symbolic directory with pathlib - python-3.x

When executing a Python 3 script in a symbolic directory, I want to get the non-dereferenced path of the current directory. However, the default behavior of pathlib returns the derefenced path:
$ mkdir test1
$ ln -s test1 test2
$ cd test2
$ pwd
/home/myuser/test2
$ ipython3
Python 3.7.0 (default, Oct 9 2018, 10:31:47)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.1.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: from pathlib import Path
In [2]: str(Path.cwd())
/home/myuser/test1
The behavior I want is to get "/home/myuser/test2" as that's where the 'script' (interpreter in this case) was executed from, preferably using pathlib.

Use os.getenv('PWD'):
Directories:
$ ls -l
total 1
drwxr-xr-x+ 1 cody agroup 0 Dec 11 15:23 dir1
lrwxrwxrwx 1 cody agroup 4 Dec 11 15:23 dir2 -> dir1
Result from dir2:
>>> str(Path.cwd())
'/home/cody/so/dir1'
>>> os.getenv('PWD')
'/home/cody/so/dir2'

Related

Ubuntu how to change from python3.10 to python3.9?

I have python 3.10 installed. But I am trying to use a python script which is only compatible with python 3.9.
So I have installed 3.9.
wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tar.xz
tar -xf Python-3.9.0.tar.xz
cd Python-3.9.0
./configure
sudo make altinstall
Python 3.9 is now installed, so I have bother 3.9 and 3.10 installed.
When I search how to use 3.9 most results say to delete 3.10.
Is there an option to temprarily switch to 3.9? So after can switch back to 3.10?
Thanks
When you have:
$ ls -l /usr/bin/python3.10
-rwxr-xr-x 1 root root 5921160 Nov 14 17:10 /usr/bin/python3.10
$ ls -l /usr/local/bin/python3.9
-rwxr-xr-x 1 root root 15651728 Dec 31 15:58 /usr/local/bin/python3.9
Then you can do:
sudo update-alternatives --install /usr/bin/python3 python /usr/local/bin/python3.10 0
sudo update-alternatives --install /usr/bin/python3 python /usr/local/bin/python3.9 0
After this being done, this should work:
$ python3
Python 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
$ sudo update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python3).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/python3.10 0 auto mode
1 /usr/bin/python3.10 0 manual mode
2 /usr/local/bin/python3.9 0 manual mode
Press <enter> to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/local/bin/python3.9 to provide /usr/bin/python3 (python) in manual mode
$ python3
Python 3.9.0 (default, Dec 31 2022, 15:24:44)
[GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
$
After the update-alternatives process, you should find this link:
$ ls -l /usr/bin/python3
lrwxrwxrwx 1 root root 24 Dec 31 16:17 /usr/bin/python3 -> /etc/alternatives/python
and in '/etc/alternatives' a link, named python which points to:
python -> /usr/local/bin/python3.9
or
python -> /usr/bin/python3.10
NOTE: You need to do the same for pip (or pip3), and other tools that relate to python3

Uninstalling python3 on MacOs

I have uninstalled python3 using brew on macOS Catalina 10.15.3:
brew uninstall python3
brew list | grep python
python#2
But it still there:
which python3
/usr/bin/python3
python3 -V
Python 3.7.3
/usr/bin/python3 -V
Python 3.7.3
ls -ltah /usr/bin/ | grep python
-rwxr-xr-x 1 root wheel 31K Jan 23 16:58 python3
lrwxr-xr-x 1 root wheel 75B Dec 16 06:20 python2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x 1 root wheel 82B Dec 16 06:20 python-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
lrwxr-xr-x 1 root wheel 76B Dec 16 06:20 pythonw2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7
lrwxr-xr-x 1 root wheel 82B Dec 16 06:20 python2.7-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
lrwxr-xr-x 1 root wheel 76B Dec 16 06:20 pythonw -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7
lrwxr-xr-x 1 root wheel 75B Dec 16 06:20 python2 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x 1 root wheel 75B Dec 16 06:20 python -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
It's interesting but on error it shows different location of python:
python3 -bla
Unknown option: -l
usage: /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/Resources/Python.app/Contents/MacOS/Python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Try `python -h' for more information.
Seems it's related to Xcode.
How to delete this version python3 and reinstall python using brew?
Update:
sudo rm -f /usr/bin/python3
rm: /usr/bin/python3: Operation not permitted
Catalina comes with python3 installed by default**. You can't delete it, because /usr/bin is on the System's read-only volume.
If you install your new python to a bin folder that comes before /usr/bin in your $PATH, then it should take precedence.
** (Well OK, the first time you run it, you have to install Xcode command line tools to make it work. But that file itself is a system install.)

Introductory lab on linux with svn

https://piazza-resources.s3.amazonaws.com/j6zdjr2o14g248/j7hl17d3fku6te/lab1.pdf?AWSAccessKeyId=AKIAIEDNRLJ4AZKBW6HA&Expires=1505775332&Signature=0n1JbqGf5F%2BRtA%2FFmqEgQcdi6iQ%3D
I don't know how to "4. Commit this new file to your repository" (step 3 part 4)
Below is everything I did up to step 3 part 4
>>> cd /cmshome/myutorid/cscb07f17_space
>>> mkdir ./lab1
>>> cd lab1
>>> mkdir ./.myrepo
>>> svnadmin create ./.myrepo
>>> pwd
/cmshome/myutorid/cscb07f17_space/lab1
>>> mkdir ./myCode1
>>> svn co file:///cmshome/myutorid/cscb07f17_space/lab1/.myrepo myCode1
Checked out revision 0.
>>> cd myCode1
>>> ls -al
total 0
drwx------ 1 myutorid cmsusers 76 Sep 18 16:01 .
drwx------ 1 myutorid cmsusers 104 Sep 18 16:00 ..
drwx------ 1 myutorid cmsusers 204 Sep 18 16:01 .svn
>>> ls -a
. .. .svn
>>> touch A.txt
>>> pico A.txt
Use "fg" to return to nano.
[1]+ Stopped pico A.txt
Could someone tell me how to commit A.txt into the repository? Does it matter what directory im stationed in?
You will need to place the file under revision control, for example:
svn add A.txt
Then you commit changes to the repository using commit, for example:
svn commit A.txt
The examples above are executed in the same directory as the file, but svn will deduce the location of the repository as long as commands are executed within the working copy (anywhere within myCode1).
The commit command requires an editor to be configured, alternatively you will need to use the -m option to provide a commit message.
By the way, [1]+ Stopped pico A.txt means pico is still running and the file has not necessarily been saved and closed. Is that what you intended?
See the online version of the manual for more.

hard-coded bin path and NixOS

When I try to do a manual source install of some software on NixOS 15.09, I get (sh -x is to get an exec log):
[nix-shell:/tmp/nix-shell-es/EventStore-oss-v3.5.0-src]$ sh -x scripts/build-js1/build-js1-linux.sh werror=no
...
+ CXXFLAGS=-fPIC
+ make x64.release werror=no
PYTHONPATH="/tmp/nix-shell-es/EventStore-oss-v3.5.0-src/scripts/build-js1/v8/tools/generate_shim_headers:" \
GYP_GENERATORS=make \
build/gyp/gyp --generator-output="out" build/all.gyp \
-Ibuild/standalone.gypi --depth=. \
-Dv8_target_arch=x64 \
-Dv8_optimized_debug=0 \
-S.x64.release -Dv8_enable_backtrace=1 -Dwerror='' -Darm_fpu=default -Darm_float_abi=default
/bin/sh: build/gyp/gyp: /bin/bash: bad interpreter: No such file or directory
Makefile:389: recipe for target 'out/Makefile.x64.release' failed
make: *** [out/Makefile.x64.release] Error 126
And indeed there is no bash program in /bin/
[nix-shell:/tmp/nix-shell-es/EventStore-oss-v3.5.0-src]$ ls -la /bin/
total 12
drwxr-xr-x 2 root root 4096 Feb 24 12:25 .
drwxr-xr-x 18 root root 4096 Feb 24 12:16 ..
lrwxrwxrwx 1 root root 63 Feb 24 12:25 sh -> /nix/store/l80ddf18bbig2icv6cmgjfws9a2vm3jj-bash-4.3-p42/bin/sh
However when I inspect the script I dont see any mention of it so it must be some subprogram looking for it.
As an aside, I imagine that's probably why the script wisely starts with #!/usr/bin/env bash and not the direct location of bash.
Anyone knows a blessed way to deal with this ?
one has to run patchShebangs ./scripts/build-js1/build-js1-linux.sh on files coming from source when they are not in the store and we want to execute them.

Can't locate Spreadsheet/ParseExcel.pm in #INC though it's seems to be installed

Can't locate Spreadsheet/ParseExcel.pm in #INC (#INC contains: /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .)
though it's installed:
% perl -e'use CPAN; install "Spreadsheet:WriteExcel"'
Going to read '/home/aspect_mkn8rd/.cpan/Metadata'
Database was generated on Sun, 27 Apr 2014 09:41:02 GMT
Spreadsheet::WriteExcel is up to date (2.40).
That's where it located, yep?
% ls -la /usr/local/lib/perl/5.14.2/auto/Spreadsheet/WriteExcel
total 12
drwxrwxr-x 2 root root 4096 апр. 28 13:11 .
drwxrwxr-x 3 root root 4096 апр. 28 13:11 ..
-rw-rw-r-- 1 root root 2519 апр. 28 13:11 .packlist
% cat /usr/local/lib/perl/5.14.2/auto/Spreadsheet/WriteExcel/.packlist
/usr/local/bin/chartex
/usr/local/man/man1/chartex.1p
/usr/local/man/man3/Spreadsheet::WriteExcel.3pm
/usr/local/man/man3/Spreadsheet::WriteExcel::BIFFwriter.3pm
<..>
/usr/local/man/man3/Spreadsheet::WriteExcel::Workbook.3pm
/usr/local/man/man3/Spreadsheet::WriteExcel::Worksheet.3pm
/usr/local/share/perl/5.14.2/Spreadsheet/WriteExcel.pm
/usr/local/share/perl/5.14.2/Spreadsheet/WriteExcel/BIFFwriter.pm
/usr/local/share/perl/5.14.2/Spreadsheet/WriteExcel/Big.pm
<..>
/usr/local/share/perl/5.14.2/Spreadsheet/WriteExcel/Workbook.pm
/usr/local/share/perl/5.14.2/Spreadsheet/WriteExcel/Worksheet.pm
% perldoc -l Spreadsheet::WriteExcel
/usr/local/share/perl/5.14.2/Spreadsheet/WriteExcel.pm
So why? What should I do?
Spreadsheet::ParseExcel and Spreadsheet::WriteExcel are different modules.
Have you installed the former too?
If not, just do the following:
perl -e'use CPAN; install "Spreadsheet::ParseExcel"'
There are 2 ways to go about the same:
use lib '/path/to/directory' where the module is stored
perl -I /path/to/directory test.pl where you include the module using the command line option -I
For more information, please refer to
https://perlmaven.com/how-to-change-inc-to-find-perl-modules-in-non-standard-locations .
Voilá, here you go, happy coding

Resources