I am trying to use Pycharm to develop Libreoffice python Macro with in the virtual environment with not a lot of success. You'll find below a few things I have tried with the differnt errors I have had so far. I welcome any answers on the errors below as well as alternate solutions.
first try : set up directly the venv environment
As when you work with Libreoffice, you need to use the embedded python integrator, the first attempt is when defining the python integrator in pycharm, I reference this embedded interpreter (located somewhere in Program Files\Libreoffice\program` on my windows machine, I click the new venv box in pycharm and get the following error :
Error: standard python 'venv' module not found
So it seems venv is not packaged with the Libreoffice embedded python environment. Is there a way to add it?
Second try : inject embedded environment inside a precreated venv
What I did here was to create the Venv in pycharm using a standard python version aligned with the one libreoffice uses (here 3.8.10). Once the venv is succesfully created, I manually change the path to the python based interpretor to the proper libreoffice one.
I also update the pythonpath in pycharm for this venv to include Files\Libreoffice\program\ so that we can find some preinstalled modules suitable for Libreoffice, in particular uno.py.
This looks a little better, I can launch a libreoffice instance, but when I actually want to do stuff inside this instance from python, I get the following error loading component library </mergedlo.dll> failed
This mergedlo.dll file is in a directory referenced in the python path for this venv.
What configuration did I miss here?
Related
Ideally I would like to use Loading an Lmod module using VSCode with Remote-SSH
however the soultion to use that directly does not seem to work. On the other hand virtualenvs ought to be supported by the Python VSCode extension.
Thus I figured the following steps should work instead.
Set-up the virtualenvironment in the remote environment
$ module load Python
$ virtualenv --system-site-packages my_python
Connect via "Remote - SSH" plugin
In the terminal source your virtual environment from 1 and get the path to the python binary softlink:
$ source my_python/bin/activate
$ which python
/path/to/my_python/bin/python
Copy the path to the python binary and paste it into the path to the Python intepreter that VSCode asks for.
And they do, but not always. The first time I tried this it worked and now everytime I try this for the same host it works directly. However, whenever I change to another host with the same parallel file system and redo step 4 it doesn't and instead says
Invalid python interpreter selected
This is the same error message as when you try to directly point to the python that the is loaded with Lmod (and built with EasyBuild if that makes a difference).
Now I'm stuck because the error message is not very helpful, I haven't found a stacktrace or any log-files that include this error. If I could find that or the code that handles the logic for what is a valid interpreter, that would be really helpful.
In summary:
virtualenv --system-site-packages symlinked Python doesn't work reliably as interpreter when system site Python is not a valid Python interpreter.
How to get this to work reliably?
Alternatively, what is the logic that determines a valid interpreter?
I'm using vscode on macOS and using nix to manage my environment. I do have python3.9 installed via homebrew as my system-default python, but I'm working on a project using a nix environment that is setting my path such that the only python executable available is python 3.8.
$ which python
/nix/store/mr2p1ycgj3wfppbw8ry2wwxg9r0xjz0z-python3-3.8.8-env/bin/python
$ whence python
/nix/store/mr2p1ycgj3wfppbw8ry2wwxg9r0xjz0z-python3-3.8.8-env/bin/python
$ whence python3
/nix/store/mr2p1ycgj3wfppbw8ry2wwxg9r0xjz0z-python3-3.8.8-env/bin/python
$ code .
The VSCode instance that comes up finds my 3.9 install as the default interpreter...
This causes a problem because the interpreter that's put on the path by nix is also the one with all my packages installed for this particular project, including developer utilities like black, flake8, pylint, etc... and my default 3.9 install does not necessarily have those.
It seems like there's a mechanism by which the Python extension discovers various python installs (I see in the extension output things where it tries to source virtualenvs and query pyenv), so what I think is happening is that it's finding a bunch of different python interpreters and blindly defaulting to the newest.
Is there any way to override the way the Python extension discovers python installs? I just want it to use the python interpreter that it finds on the path by default. I know I can override the interpreter per-project via the .vscode/settings.json, but that's a bit tedious to do for every project, and the way nix works that path may change occasionally. I already have "python.pythonPath": "python" in my vscode settings (which is the default anyway) - I was hoping that would work... it does not.
When we use "code." to open VS Code from the system terminal outside VS Code, it uses the latest version (highest version) of python installed in the system by default.
For directly opening an existing folder (containing python files) from VS Code, it defaults to using the python environment used last time, and for a new project that has not been selected, it also defaults to using the latest version (highest version) of python installed in the system .
You could click on the python environment in the lower left corner of VS Code, and then switch to the desired python environment in the options. In addition, it is recommended that you open the previously used project from VS Code (click File, Open Folder) and use the virtual environment in VS Code.
I've installed Python's latest version. However, when I write in command prompts
python --version
I get:
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
I tried downloading it directly from the Microsoft Store, however, the problem with that was that I could not very easily create a path for it and ran out of skills.
Anyways I'm trying to get Robot Framework to run just for fun, I'm a complete beginner as you can probably see. What should I do to fix this?
You have to add the path of Python to the environment of your system.
For more details refer: How to add Python to Windows PATH
How to add Python to Windows PATH
There are few ways in which you can add Python to Windows PATH. In this guide, I’ll share with you two methods that you can use to add Python to Windows path:
Via the installation of a recent version of Python
Manual entry of the paths
But why would you want to add Python to Windows path in the first place?
Well, if you try to install a Python package using PIP for example, you may get the following error in the Windows Command Prompt:
‘pip’ is not recognized as an internal or external command, operable program or batch file
To overcome this error, you may apply any of the two methods described below.
Method 1: Install a Recent Version of Python
You can easily add Python to Windows path by downloading a recent version of Python, and then checking the box to Add Python to PATH during the installation.
Before you proceed, you may choose to uninstall your previous version of Python if needed.
In my case, the latest version of Python that was available to download was version 3.7.2.
In the Python installation box, just check the box to add Python to PATH as below:
How to add Python to Windows PATH
Finish the installation, and you should be good to go.
Alternatively, you may manually add the paths into the Environment variables.
Method 2: Manually add Python to Windows Path
If you wish to stick with your previous version of Python, you may apply the steps below to manually add Python to Windows path.
Note that I’ll be using Windows 10 to demonstrate the steps, but similar principles would apply for previous versions of Windows.
Step 1: Navigate to the Windows Environment Variables screen
To navigate to the Windows Environment Variables screen, where you can add/edit your paths, simply right click on the ‘This PC‘ icon. Then, select ‘Properties.’
This PC - Windows 10
Next, click on the ‘Advanced system settings‘
Advanced system settings
Finally, click on the ‘Environment Variables…‘
Environment variables
That should take you to the Environment Variables screen, where you can add/edit your paths.
Click on ‘New…‘ to add the ‘Path’ variable (note that if your ‘Path’ variable already exists, then click on ‘Edit…’ instead):
add Python to path
You should then see the following box, which will allow you to add/edit variables:
New User Variable
Before you type any values, you’ll need to locate the relevant Python paths. The paths that you’ll need to get are:
The Python application path, which is the folder where you originally installed Python; and
The Python Scripts path. The Scripts folder should be located within the Python application path.
Here is how my Python application path looks like:
Python application path
And this is how my Python Scripts path looks like:
Python Scripts path
Now let’s fill the New User Variable box that you saw earlier:
New User Variable
For the Variable name, type ‘Path‘.
For the Variable value, copy the full Python application path, then use semicolon (as highlighted in yellow below), and finally copy the Python Scripts path.
This is how my Variable value looks like:
C:\Users\Ron\AppData\Local\Programs\Python\Python37-32;C:\Users\Ron\AppData\Local\Programs\Python\Python37-32\Scripts
Put all the values together in the New User Variable box:
New User Variable - Windows 10
Press ‘OK’ and you would then see your new Python Path under the ‘User variables’ section. Don’t forget to press ‘OK’ again so that the changes will get implemented.
How to add Python to Windows PATH
That’s it! You just added Python to the Windows Path.
You’ll now be able to install Python packages easily, by opening the Windows Command Prompt and then typing:
pip install package name
For example, to install the pandas package, simply type ‘pip install pandas’ and then press Enter:
How to add Python to Windows PATH
Similarly, you may upgrade PIP by typing the following command:
python -m pip install --upgrade pip
I am using Python 3.7 on windows. I installed Pycharm and successfully wrote a script which I am now trying to schedule using Windows Task Scheduler, which comes with its on set of complications. One thing I have noticed about Pycharm is I think it has created a separate file directory to store any packages I add to a script (maybe in something called "venv"? Instead of using the User/Python37/Scripts file.
This means when I try to run my script in the command prompt, python.exe looks for packages and cannot find them. Also if I go into my Pycharm project folder is see another instance of a Python Application file different than the Python Application stored in User/Python37. I think this also creates problems but I am not 100% sure.
I am hoping someone has seen this issue and can help me align where Pycharm stores packages. Any help would be greatly appreciated.
You can also simply add your script/package into your python path.
For that follow this awser : How to add to the PYTHONPATH in Windows, so it finds my modules/packages?
PyCharm creates a virtual environment (venv) where you can keep the python version and the libraries used in a specific project.
You can add libraries to the specific environment through the Pycharm GUI:
File > Settings > Project: Patterns > Project Interpreter > Install (green +)
Find your package and click Install Package in your venv.
You can see all the installed packages and their version in the path:
File > Settings > Project: Patterns > Project Interpreter
You can also use pip install, if you want to go through CLI, but be sure to use the virtual environment's pip (located in project_folder/venv/Scripts).
If for some reason you want to use the python version outside the virtual environment, go to the following path in PyCharm:
File > Settings > Project: Patterns > Project Interpreter
In the Project interpeter dropdown menu, you should find other python's location; choose the one you prefear. If you don't see your standard python version (usually in C:\python\python.exe, or something similar), you can add it by clicking on the settings menu, and specify the path to the desired python version in Base interpreter:.
In this window, you can find other settings to configure the interpreter as you want.
I tried to open a python script with VS code. The library that requires with the script is in a virtualenv.
I am not sure how to integrate this with VS Code. I could not find my virtualenv in the intepreter.
For your information, the virtualenv I created is called "vc-web" and the scripts is in the folder called "web_scraper" inside the "vc-web" folder.
If your project is in folder e.g. proj, and you have a virtual environment at proj/vs-web and it's the only virtual environment in that directory, then it should be listed by the Python: Select Interpreter command where you can select it.
All you need to do is just specify the "python.pythonPath" as follows. The values for "python.autoComplete.extraPaths" will be determine during runtime, however you are still free to specify custom paths in there.
Please remember to re-start VS Code once the necessary changes have been made.
{
"editor.rulers": [80,100],
"python.pythonPath":"~/path/to/venvs/proj/bin/python"
}