How to combine two versions of the python3 - python-3.x

I need some advice.
I have a custom module requires version Python3.5 during installation or later versions. (no critical for run my script)
My script will simultaneously use "some_decoder.pyc" file which was compiled under Python3.3 (legacy) and need this version for run. (critical for run)
How I can to solve this problem if I use Linux and virtualenv?
I have one idea in two steps:
I install basic module under global environment (Python3.5).
I will create an environment which inherit basic modules but use python3.3 (in Windows I was able to do so)
But I hope that this problem can be solved in a different way, or redirect me where this problem was solved.

Related

How to embed FreeCAD in a Python virtual environment?

How do I setup a Python virtual environment with the FreeCAD library embedded as to enable import as a module into scripts?
I would like to avoid using the FreeCAD GUI as well as being dependent on having FreeCAD installed on the system, when working with Python scripts that use FreeCAD libraries to create and modify 3D geometry. I hope a Python virtual environment can make that possible.
I am working in PyCharm 2021.1.1 with Python 3.8 in virtualenv on Debian 10.
I started out with FreeCAD documentation for embedding in scripts as a basis:
https://wiki.freecadweb.org/Embedding_FreeCAD
In one attempt, I downloaded and unpacked .deb packages from Debian, taking care to get the correct versions required by each dependency. In another attempt, I copied the contents of a FreeCAD flatpak install, as it should contain all the libraries that FreeCAD depends on.
After placing the libraries to be imported in the virtual maching folder, I have pointed to them with sys.path.append() as well as PyCharm's Project Structure tool in various attempts. In both cases the virtual environment detects where FreeCAD.so is located, but fails to find any of its dependencies, even when located in the same folder. When importing these dependencies explicitly, each of them have the same issue. This leads to a dead end when an import fails because it does not define a module export function according to Python:
ImportError: dynamic module does not define module export function (PyInit_libnghttp2)
I seem to be looking at a very long chain of broken dependencies, even though I make the required libraries available and inform Python where they are located.
I would appreciate either straight up instructions for how to do this or pointers to documentation that describes importing FreeCAD libraries in Python virtual environments, as I have not come across anything that specific yet.
I came across a few prior questions which seemed to have similar intent, but no answers:
Embedding FreeCAD in python script
Is it possible to embed Blender/Freecad in a python program?
Similar questions for Conda focus on importing libraries from the host system rather than embedding them in the virtual environment:
Incude FreeCAD in system path for just one conda virtual environment
Other people's questions on FreeCAD forums went unanswered:
https://forum.freecadweb.org/viewtopic.php?t=27929
EDIT:
Figuring this out was a great learning experience. The problem with piecing dependencies together is that for that approach to work out, everything from the FreeCAD and its dependencies to the Python interpreter and its dependencies seems to need to be built on the same versions of the libraries that they depend on to avoid causing segmentation faults that brings everything to a crashing halt. This means that the idea of grabbing FreeCAD modules and libraries it depends on from a Flatpak installation is in theory not horrible, as all parts are built together using the same library versions. I just couldn't make it work out, presumably due to how the included libraries are located and difficulty identifying an executable for the included Python interpreter. In the end, I looked into the contents of the FreeCAD AppImage, and that turned out to have everything needed in a folder structure that appears to be very friendly to what PyCharm and Python expects from modules and libraries.
This is what I did to get FreeCAD to work with PyCharm and virtualenv:
Download FreeCAD AppImage
https://www.freecadweb.org/downloads.php
Make AppImage executable
chmod -v +x ~/Downloads/FreeCAD_*.AppImage
Create folder for extracting AppImage
mkdir -v ~/Documents/freecad_appimage
Extract AppImage from folder (note: this expands to close to 30000 files requiring in excess of 2 GB disk space)
cd ~/Documents/freecad_appimage
~/Downloads/./FreeCAD_*.AppImage --appimage-extract
Create folder for PyCharm project
mkdir -v ~/Documents/pycharm_freecad_project
Create pycharm project using Python interpreter from extracted AppImage
Location: ~/Documents/pycharm_freecad_project
New environment using: Virtualenv
Location: ~/Documents/pycharm_freecad_project/venv
Base interpreter: ~/Documents/freecad_appimage/squashfs-root/usr/bin/python
Inherit global site-packages: False
Make available to all projects: False
Add folder containing FreeCAD.so library as Content Root to PyCharm Project Structure and mark as Sources (by doing so, you shouldn't have to set PYTHONPATH or sys.path values, as PyCharm provides module location information to the interpreter)
File: Settings: Project: Project Structure: Add Content Root
~/Documents/freecad_appimage/squashfs-root/usr/lib
After this PyCharm is busy indexing files for a while.
Open Python Console in PyCharm and run command to check basic functioning
import FreeCAD
Create python script with example functionality
import FreeCAD
vec = FreeCAD.Base.Vector(0, 0, 0)
print(vec)
Run script
Debug script
All FreeCAD functionality I have used in my scripts so far has worked. However, one kink seems to be that the FreeCAD module needs to be imported before the Path module. Otherwise the Python interpreter exits with code 139 (interrupted by signal 11: SIGSEGV).
There are a couple of issues with PyCharm: It is showing a red squiggly line under the import name and claiming that an error has happened because "No module named 'FreeCAD'", even though the script is running perfectly. Also, PyCharm fails to provide code completion in the code view, even though it manages to do so in it's Python Console. I am creating new questions to address those issues and will update info here if I find a solution.

How to chose which 3.x python version on cmd?

I have some code working with python 3.6.6 but I have 3.7.2 in enviroment too.When I try to run my code in cmd, it's trying to run with 3.7.2.How can I chance it?
There are a couple of ways you can tell a script to run with a specific version of Python, commonly you'd use a shebang:
#!/user/bin/env/python3.x
However, I think this only works for Linux/Mac which it sounds like you aren't on if you're referring to cmd. You may want to check out this answer which has a few other options that may help: How do I tell a Python script to use a particular version
Alternatively you can set up a virtual environment, which allows you to run different versions of Python, modules and any other environment settings for a specific project. If that sounds like a better solution then this answer should help you out: (Easiest) Way to use Python 3.6 and 3.7 on same computer?

Install an external library instance of libv8-3.14 to folder

I need libv8-3.14 to run some R packages on linux, but I don't have root access/sudo access on the linux computer I'm using so I'd like to install an external folder instance of libv8-3.14. I've seen R packages reference this as external as CDFLAG="folder/v8-3.14" so I know it is possible.
I'm new(ish) to linux but I've installed external libraries before with tar.gz files which then have a configure file in them, which I set the external folder with ./configure --prefix==/folder/loc, but the only downloads I can find of libv8 are .git (which I can't get to work either).
How can I install an libv8-3.14 to a folder and install so I can set:
export PATH=$PATH:/path/to/install/
and
export `LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/install/`
I had the exact same problem. In case somebody in the future comes across this post, I will leave my suggestions and how it worked out in the end. Also, all credits go to an experienced colleague of mine.
The most sure thing to do is to consult IT, or someone who has already had the same problem, there is usually a workaround these issues.
A way you can do it yourself:
Create an anaconda environment, you can name it 'V8' or something (make sure the environment is based on the latest python version, or recent enough for r-v8).
activate it
install the conda version of the V8 R interface with conda install -c conda-forge r-v8
That's it. Whenever you need V8, fire up your environment beforehand, and it should be A-OK.
Further advice: If you run into errors when installing r-v8, it may be a good idea to update your conda and all the packages. However, depending on your conda version conda update conda and conda upgrade --all MAY BREAK your conda installation, so be careful. (For further information on this problem, see the endless complaints of people in this issue: https://github.com/conda/conda/issues/8920).
V8 doesn't use autotools, so it has no ./configure. In fact, it provides no installation facilities at all, because it is meant for embedding, not installing.
What I would try is to download the Ubuntu package (guessing from your other question, you are on Ubuntu, right?) for the right architecture from https://packages.ubuntu.com/trusty/libv8-3.14.5, and extracting it manually. .deb files are just ZIP archives.
As a side note, there's no point in setting PATH, because libv8, being a library, provides no executables. LD_LIBRARY_PATH is all you need.

Set the PYTHONPATH for a older version of Python to a directory where I want to install it though I have a default version of Python3

I am using ubuntu 18.04. I have Python-3.6.6 installed as a default version. But for practice I have installed Python-3.6.5 from source code to a directory where I wanted to install it and changed the .bashrc file accordingly. But it is showing me the default python3 path when I call "which" command.
How to use the python version which one I have installed recently?
If you only want to run some practice scripts you can select directly which python you want to use for your script. In the command line, e.g. type
PYTHONPATH=/foo/bar/python365 python somescript.py
where /foo/bar/python365 points to your python-3.6.5
You can also change your PYTHONPATH variable to look first in the python-3.6.5 folder before it looks to the default python (3.6.6) you had installed before. However, I do not recommend this, since it will make all python applications choose the python-3.6.5 as default. For extended practice with python-3.6.5 consider a virtual environment or an anaconda environment.
You should use virtualenv or something similar, like pipenv, I myself use virtualenv.
Here's a guide on how to do that: https://docs.python-guide.org/dev/virtualenvs/
virtualenv is a bit lower level and so not that intuitive to use, but since I used virtualenv before I found pipenv, I just stuck with it, while adding a few aliases to my .bashrc to make it easier.
I used two different python versions because discord.py would break on 3.7 (that doesn't seem to be the case anymore, but I still have it setup with 3.6 and 3.7), so here's what my .bashrc for two different versions looked like, this is on windows, but if you are on unix system or macOS (I want to make this answer as general as possible, it works the same on all operating systems really), just change the paths to lead to your interpreters (whichever you have) and don't use winpty:
alias python36="winpty C:/Users/Hevaesi/AppData/Local/Programs/Python/Python36-32/python.exe"
alias python37="winpty C:/Users/Hevaesi/AppData/Local/Programs/Python/Python37-32/python.exe"
alias pip36="C:/Users/Hevaesi/AppData/Local/Programs/Python/Python36-32/python.exe -m pip"
alias pip37="C:/Users/Hevaesi/AppData/Local/Programs/Python/Python37-32/python.exe -m pip"
alias venv36="python36 -m virtualenv"
alias venv37="python37 -m virtualenv"
alias python="winpty python"
Now, to use Python 3.6, I use venv36 venv in my project folder, and it setups Python 3.6 environment, using venv37 venv would create Python 3.7 environment.
I also wrote a function to make activating less of a hassle (again, this is in bashrc)
activate() {
if [ "$#" == 1 ]; then
source "$1/Scripts/activate"
else
echo "Usage: activate <env_name>"
fi
}
So, after I created my env once, I go to project folder, type activate venv and it starts up whatever version I wanted the project to have all by itself, to run my project I just use python myfile.py, when virtual environment is activated, it will use the interpreter that's in active virtual environment, regardless of what you have in your PATH variable. Also, using pip will automatically install to your virtual environment, not your actual installation, meaning global package list will be clean, or in best case, completely empty.
You should only install what you need and keep it separate environments, normally you use a single environment per project. Well, unless your project is client/server at once (for whatever reason) and server uses different libraries than client, for example, more of them, client environment could have less modules since that's what the server is for, to provide something without having to do it yourself, so they could be two projects in one, hence two different environments, separating dependencies based on what the "unit" in the project really needs.
Note: you have to install virtualenv (or pipenv or whatever you want to use really) into all of your python installations, if you want to create virtual environments from them, that's what pip36 and pip37 here is for - for installing packages directly into actual python installations, for now, I only had to do pip36 install virtualenv and pip37 install virtualenv.

unable to execute 'x86_64-conda_cos6-linux-gnu-gcc': No such file or directory (pysam installation)

I am trying to install pysam.
After excecuting:
python path/to/pysam-master/setup.py build
This error is produced:
unable to execute 'x86_64-conda_cos6-linux-gnu-gcc': No such file or directory
error: command 'x86_64-conda_cos6-linux-gnu-gcc' failed with exit status 1
There are similar threads, but they all seem to address the problem assumig administriator rights, which I do not have. Is there a way around to install the needed files?
DISCLAIMER: This question derived from a previous post of mine.
manually installing pysam error: "ImportError: No module named version"
But since it might require a different approach, I made it a question of its own.
You can also receive the same error while installing some R packages if R was installed using conda (as I had).
Then just install the package by executing: conda install gxx_linux-64 to have that command available.
Source:
https://github.com/RcppCore/Rcpp/issues/770#issuecomment-346716808
It looks like Anaconda had a new release (4.3.27) that sets the C compiler path to a non-existing executable (quite an embarrassing bug; I'm sure they'll fix it soon). I had a similar issue with pip installing using the latest Miniconda, which I fixed by using the 4.3.21 version and ensuring I was not doing something like conda update conda.
See https://repo.continuum.io/miniconda/ which has release dates and versions.
It should now be safe to update conda. This is fixed in the following python packages for linux-64:
python-3.6.2-h0b30769_14.tar.bz2
python-2.7.14-h931c8b0_15.tar.bz2
python-2.7.13-hac47a24_15.tar.bz2
python-3.5.4-hc053d89_14.tar.bz2
The issue was as Jon Riehl described - we (Anaconda, formerly Continuum) build all of our packages with a new GCC package that we created using crosstool-ng. This package does not have gcc, it has a prefixed gcc - the missing command you're seeing, x86_64-conda_cos6-linux-gnu-gcc. This gets baked into python, and any extension built with that python goes looking for that compiler. We have fixed the issue using the _PYTHON_SYSCONFIGDATA_NAME variable that was added to python 3.6. We have backported that to python 2.7 and 3.5. You'll now only ever see python using default compilers (gcc), and you must set the _PYTHON_SYSCONFIGDATA_NAME to the appropriate filename to have the new compilers used. Setting this variable is something that we'll put into the activate scripts for the compiler package, so you'll never need to worry about it. It may take us a day or two to get new compiler packages out, though, so post issues on the conda-build issue tracker if you'd like to use the new compilers and need help getting started.
Relevant code changes are at:
py27: https://github.com/anacondarecipes/python-feedstock/tree/master-2.7.14
py35: https://github.com/anacondarecipes/python-feedstock/tree/master-3.5
py36: https://github.com/anacondarecipes/python-feedstock
The solution that worked for me was to use the conda to install the r packages:
conda install -c r r-tidyverse
or r-gggplot2, r-readr
Also ensure that the installation is not failing because of admin privileges.
It will save you a great deal of pain
After upgrading Golang to 1.19.1, I started to get:
# runtime/cgo
cgo: C compiler "x86_64-conda-linux-gnu-cc" not found: exec: "x86_64-conda-linux-gnu-cc": executable file not found in $PATH
Installing gcc_linux-64 from the same channel, has resolved it:
conda install -c anaconda gcc_linux-64
Somewhere in your $PATH (e.g., ~/bin), do
ln -sf $(which gcc) x86_64-conda_cos6-linux-gnu-gcc
Don't put this in a system directory or conda's bin directory, and remember to remove the link when the problem is resolved upstream. gcc --version should be version 6.
EDIT: I understand the sentiment in the comments against manipulating system paths, but maybe we can use a little critical thinking for the actual case in hand before reciting doctrine. What actually have we done with the command above? Nothing more than putting an executable (symlink) called x86_64-conda_cos6-linux-gnu-gcc in one's personal ~/bin directory.
If putting something in one's personal ~/bin directory broke future conda (after it fixes the C compiler path to point to gcc it embeds), then that would be a bug with conda. Would the existence of this verbosely named compiler mess with anything else? Unlikely either. Even if something did pick it up, it's just your system gcc after all...

Resources