I am pretty new to python, interpreters, and programming in general. I script my .py files in pycharm. As you probably know pycharm copies the default interpreter to every project. When I install external libraries I install them at the project interpreter, not at the main interpreter. That works well during the pycharm development phase, but when I try to run the script outside it, it runs it with the default interpreter ( without the newly downloaded libraries ). How can I change that every script uses his own interpreter?
I don't want to download all these libraries to the default compiler and I don't want to change the default library every time I run another script ( via variable editor ).
Also I don't want ro run all my files using pycharm everytime ( since it's quite resource consuming and it takes a while )
I haven't tried anything yet, I couldn't find info about this.
When running without pycharm:
Traceback (most recent call last):
File "C:\Users\Andrei\PycharmProjects\mcdis\mcdis.py", line 1, in <module>
import discord
ModuleNotFoundError: No module named 'discord'
PS: The OS is Windows 10.
I found a shortcut:
Using a shebang ( a line of 'code' that specifies the interpreter which should be used ). While virtual environments seem to have some advantages the shebang trick should do it for most of the scripts.
How to use a shebang :
#!"C:\Users\Andrei\Desktop\MCSERVER\venv\Scripts\python.exe"
Just tell the path to the interpreter with a '#!' in front. Not sure if it works with relative path but I think it does.
Related
I am working on a Python project in Pycharm (2020.1.2) on Windows 10.
For this project, I cannot use the standard Python interpreter, I have to use my own located at C:\some\path\here\python\27_64\python.exe (Python 2.7.3).
Backstory may be important:
I have added this path to the system path for both myself and all users, and placed it ahead of %LOCALAPPDATA%\Microsoft\WindowsApps to try to prevent the Microsoft store from popping up whenever I try to run python on the command line - however I don't feel like this change to the path variable has made a difference, as the Microsoft store still pops up.
I can start a Python shell by running C:\some\path\here\python\27_64\python.exe, so I know it technically works. When I do so; the sys.path is as follows:
['', 'C:\\another_place\\Python_2.7.3_x64\\python27.zip',
'C:\\some\\path\\here\\python\\27_64\\DLLs',
'C:\\some\\path\\here\\python\\27_64\\lib',
'C:\\some\\path\\here\\python\\27_64\\lib\\plat-win',
'C:\\some\\path\\here\\python\\27_64\\lib\\lib-tk',
'C:\\some\\path\\here\\python\\27_64',
'C:\\some\\path\\here\\python\\27_64\\lib\\site-packages']
Anyway, when I try to run a Python console (not even my script), this is the message I get in Pycharm:
C:\some\path\here\python\27_64\python.exe "C:\Program Files\JetBrains\PyCharm 2020.1.1\plugins\python\helpers\pydev\pydevconsole.py" --mode=client --port=59771
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm 2020.1.1\plugins\python\helpers\pydev\pydevconsole.py", line 5, in <module>
from _pydev_comm.pydev_rpc import make_rpc_client, start_rpc_server, start_rpc_server_and_make_client
File "C:\Program Files\JetBrains\PyCharm 2020.1.1\plugins\python\helpers\pydev\_pydev_comm\pydev_rpc.py", line 1, in <module>
import socket
File "C:\some\path\here\python\27_64\lib\socket.py", line 47, in <module>
import _socket
ImportError: DLL load failed: The specified procedure could not be found.
Process finished with exit code 1
I have gone to Settings>Project:[name]>Project Interpreter and set it to C:\some\path\here\python\27_64\python.exe (and rebooted Pycharm to be sure). That said; in the settings window no packages are shown and it claims that Python packaging tools can not be found.
When I click the link to install them (circled in red), they can not be installed due to this error:
ImportError: cannot import name _remove_dead_weakref
Are there other variables or settings I need to change?
Thanks
EDIT
Uninstalling the first Python on my Path (C:\\another_place\\Python_2.7.3_x64\\python27.zip) just makes everything so much worse
EDIT 2
I added the PATH variable manually to both the Python console settings and to the Run/Debug settings in PyCharm (and restarted the program), the result is still the same
I had a similar issue. This procedure fixed my issue.
Try the following:
run print(os.environ['PATH']) in the system terminal using the same interpreter
copy the result and add as PATH environment variable to your Run/Debug Configuration
do the same for Python Console settings
I hope it will work.
It seems that the interpreter is not being recognized by the windows as a result of which you are unable to install packages.
Also, I suppose the interpreter should be present in the bin folder of your python folder. The interpreter does not have a .exe extension.
I would suggest installing anaconda python 2.7 64 Bit windows package installer and using the condo environment and work on python 2.7
Here is a link I found for Python 2.7 on Windows hope this helps:
https://docs.python.org/2/faq/windows.html
Had the similar issue, in my case it was always trying to find libraries in PostgreSQL installation directory.
Mentioning sys.path helped me here! I tried printing it from inside my script and realized that PostgreSQL directories appear earlier in the list than Python directories.
SO, how I finally fixed it -- added PYTHONPATH environment variable to my Run configuration in PyCharm like this (replace with paths to your Python installation directory):
PYTHONPATH=D:\PROGRAMS\Python\Python3.9\DLLs\;D:\PROGRAMS\Python\Python3.9\lib\;D:\PROGRAMS\Python\Python3.9\;D:\PROGRAMS\Python\Python3.9\lib\site-packages
This helps to put desired directories at the beginning of the list, thus they are searched first and required libraries are found as it is supposed to work.
When using python packaged as embeddable zip file somewhere on a share and my python apps (there are multiple ones) somewhere else on separate shares I can version the apps separated from each other and from python. The apps then "share" (use) the same python version (embeddable instance) - so I only have to manage one python version for all of my apps.
Now I want do debug one app. (Assuming here: The app already works without errors when running it) I use eclipse and PyDev. After workspace creation and linking the code as folder into a project in the eclipse workspace I tell PyDev where to find my python embeddable interpreter and which app to launch.
When I start debugging the debugger tells me:
Traceback (most recent call last):
File "C:\eclipse\plugins\org.python.pydev.core_7.1.0.201902031515\pysrc\pydevd.py", line 19, in <module>
from _pydev_bundle import fix_getpass
ModuleNotFoundError: No module named '_pydev_bundle'
So the debugger doesn't find ist own modules?
Now I add the path of the PyDev sources to the PYTHONPATH of the embeddable python interpreter in the file
python36._pth
as follows:
C:\eclipse\plugins\org.python.pydev.core_7.1.0.201902031515\pysrc
(Yeah that path is not relative)
(Side note: I have to reconfigure the interpreter in eclipse to make pydev recognize the path changes)
Doing so resolves that problem and I can debug the app. (Ignoring warnings of the debugger that flood my console)
Can someone tell me why I need to add that path to my python embeddable which should be as independent as possible? Can I setup that somewhere else? I already tried to add the sources path to the eclipse project PYTHONPATH and the eclipse interpreter PYTHONPATH without success.
PS: My python should be independent of the apps to share it between them and to be able to exchange it (fresh upgrade to newer version).
The apps are started using a Windows power shell link where I call my python embeddable executable and pass the app as an argument. So double clicking the power shell icon is enough for app users
I didn't really understand one thing: you're launching your code from within Eclipse (say debug as > Python) and it's giving that error?
Are you changing the PYTHONPATH somehow?
The way that it works is that Eclipse/PyDev will change the PYTHONPATH environment variable to add the needed paths for the pydevd debugger, but if you have a custom Python or zip which is then replacing or not using that var, that result would be expected and you have to make sure that the PYTHONPATH env var is still respected for the debugger to work...
I have installed Eclipse IDE with Python, I'm trying to setup an environment for writing some python scripts to automate Libre Office, I have made a script with just one line of code for now ("import libpyuno"), when i run this line of code i get an error:
ImportError: dynamic module does not define init function (initlibpyuno)
I have added "usr/lib/libreoffice/program" as external library path.
Can anyone tell me why I'm getting this error?
The supported way of using pyuno is to invoke the python interpreter bundled with LibreOffice itself. If you want to use an existing interpreter, then you need to make sure you manually do the same setup as the python shell script does inside the LibreOffice installation set (/usr/lib/libreoffice) in your case. This involves not only setting PYTHONPATH but at least specifying URE_BOOTSTRAP as well (both are environment variables.)
Now to your actual question: you probably don't want to import libpyuno. If you want to import a single LibreOffice-specific Python module for testing, then import just uno.
See https://cgit.freedesktop.org/libreoffice/core/tree/scripting/examples/python/NamedRanges.py for a real-world pyuno script, you can see even that only needs the uno module.
I have a python3 script that I am calling in terminal; I do not use Python prefix to run it, since I did add #!/usr/local/bin/python3 in my script (I have python3 from brew, on OSX).
The interesting thing is that if I run the script in terminal, I get an import error because one of my custom module hasn't been found. If I run the same script in pycharm, it works fine.
I assume Python launch and read all the various path that I use for modules in the same way, in both pycharm and terminal, but it seems not the case. How do I set up my scripts so the modules are found, independently from their path?
I may run the same script from other machines too, so I want to be prepared and do the right thing from the start.
EDIT
I am running pycharm on OSX; Python3 is installed via Brew, but the symlink is in /usr/local/bin.
My script is running from a folder inside my home directory, so
/Users/tester/git/python_test_app/main/base/app_main.py
The custom modules are in the same folder of the main py script, but one level above: /Users/tester/git/python_test_app/main/pyutils.py
The import statement from app_main.py is
import main.pyutils as utilities
This is the stack trace that I get when running the script:
Traceback (most recent call last):
File "main/base/app_main.py", line 13, in <module>
import main.pyutils as utilities
ModuleNotFoundError: No module named 'main'
EDIT 2 and solution
Thanks to The answers, I was able to figure out that the issue is related to how Pycharm handle projects. Basically it somehow save the path where the project is; so calling an import will result in the project folder being parsed, and that's why it works fine from Pycharm.
In Python, unless PYTHONPATH has the path to my project or other modules that I wrote, it won't be able to find them, hence, raise the error.
FIX:
in my main module that I use to run the application, I did retrieve the path of the file; which I know being one level below the modules I need; so I can explicitly add the folder to the current sys.path. This will end up making possible for me to call the import successfully.
import sys
current_dir = os.path.dirname(__file__)
sys.path.insert(0, , current_dir)
The only downside is that every file and resource that I use in my project, has to be directly referred by full path; so I have to pass the current_dir around the various files in the project.
PyCharm has project interpreter settings. Verify these are the same as your system Python. Go to:
File menu
Settings
Project: <project name>
Project Interpreter
View the path to the Python executable/binary being used by the project in PyCharm and verify it matches what your system is calling (e.g., which python3)
Alternatively, it may be that you declared your sources root within PyCharm and the system cannot properly run the module as it exists in the path you're running it from (especially if inside a package). You can get around this using the -m parameter and calling it from Python.
You can also try running it from the Terminal inside PyCharm and see what it adds to the path before initializing the shell session (you can sometimes see this in your Run configurations also). If you are referring to modules not installed via pip / into the Python path but rather loaded into your project path, then this may be the culprit.
On PyCharm, next to the green "RUN" arrow press the box and then press edit configurations (see image)
There you'll have Working Directory - that path is where PyCharm is running that script from (without errors).
Try running it from the terminal within that path - that should solve your import errors.
I'm writing a simple test for Android app and it fails while trying to connect my device with this log:
Traceback (most recent call last): File "D:/MonkeyRunnerTest/test/LaunchTest.py", line 3, in <module>
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice ImportError: No module named 'com'
I use Pycharm 2.7.3, Python 3.3.2, juthon-standalone-2.5.3. I've tried to launch my test project on Eclipse with PyDev and Intellij Idea with Python plug-in, but with the same result. Also I've added environment variable PYTHONPATH containing the path to monkeyrunner and jython source to my operation system (Windows 7), it didn't help.
Any suggestions for this issue?
You should only use monkeyrunner interpreter to run monkeyrunner scripts. Forget about python, jython, etc.
From you command line try:
monkeyrunner LaunchTest.py
and it will work.
You can find some instructions to use monkeyrunner with Eclipse+Pydev. See the updates at the bottom of the page.
Assuming you have the proper modules installed: They aren't in your system path. You can check your system path manually to see if the directory is there by doing
import sys
print sys.path
You can append to sys.path as you would any list, but it's probably better to modify it via your OS, rather than on the fly appending. (which is temporary, sys.path reverts to its original state after the end of the script in python)