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.
Related
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.
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.
Eclipse Neon 4.6.0, PyDev 5.1.2, OS X El Capitan 10.11.6, GAE SDK 1.9.40
The project I am working on runs locally and on appspot.com, I'm trying to move to Eclipse from Pycharm to see if Eclipse works better for me. I have the PyDev plugin installed. The project works fine while running in debug mode too.
When the debugger pauses at a break point I get this warning on the console:
/Users/[my_user_id]/.p2/pool/plugins/org.python.pydev_5.1.2.201606231256/pysrc/_pydevd_bundle/pydevd_xml.py:85: RuntimeWarning: Parent module '_pydevd_bundle' not found while handling absolute import
While I have the debugger paused at any break point at all and I enter any valid or invalid command at the Python prompt I get the expect response from Python followed by this error:
Traceback (most recent call last):
File "/Users/[my_user_ID]/.p2/pool/plugins/org.python.pydev_5.1.2.201606231256/pysrc/_pydevd_bundle/pydevd_save_locals.py", line 35, in save_locals
ImportError: No module named _pydevd_bundle
My assumption is that the path to the _pydevd_bundle is somehow a problem but, the normal fixes are not getting it.
That path is:
/Users/[my_user_ID]/.p2/pool/plugins/org.python.pydev_5.1.2.201606231256/pysrc
I have checked that in Finder.
So far I have tried the following:
Reinstalled Eclipse & PyDev from alternate mirror sites in case there
was some corruption
Forced the path to the _pydevd_bundle into the system path.
At Preferences >> PyDev >> Interpreters >> Python Interpreter:
Added the path above to the PYTHONPATH list
Added all the modules under that /pysrc directory to the Forced
Builtins list
I've also tried going down to the module directory itself which
should not be needed since all the module directories have an
__init__.py file.
Before the reinstall I even tried commenting out the offending import
statement in the pydevd_save_locals.py file.
The only thing I have not tried is blowing away my entire Python install and starting from scratch with everything attached to that. Just more work with a 50/50 shot at a fix.
Since the debugger appears to work okay this might be more of an annoyance than anything else. But, it's potentially hiding other errors however. Any help would be appreciated.
Update: I upgraded the GAE SDK to 1.9.40, reinstalled Eclipse, and reinstalled PyDev, no joy.
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)
When I use the IPython included with Enthought Python Distribution, I can import the pyvision package just fine. However, when I try to import pyvision inside of PyCharm 1.2.1, I get the following errors
File "C:\Python27\lib\site-packages\pyvision\__init__.py", line 146, in <module>
from pyvision.types.img import Image,OpenCVToNumpy,NumpyToOpenCV
File "C:\Python27\lib\site-packages\pyvision\types\img.py", line 43, in <module>
import numpy
File "C:\Python27\lib\site-packages\numpy\__init__.py", line 142, in <module>
import add_newdocs
File "C:\Python27\lib\site-packages\numpy\add_newdocs.py", line 9, in <module>
from numpy.lib import add_newdoc
File "C:\Python27\lib\site-packages\numpy\lib\__init__.py", line 13, in <module>
from polynomial import *
File "C:\Python27\lib\site-packages\numpy\lib\polynomial.py", line 17, in <module>
from numpy.linalg import eigvals, lstsq
File "C:\Python27\lib\site-packages\numpy\linalg\__init__.py", line 48, in <module>
from linalg import *
File "C:\Python27\lib\site-packages\numpy\linalg\linalg.py", line 23, in <module>
from numpy.linalg import lapack_lite
ImportError: DLL load failed: The specified module could not be found.
Am I missing some path settings in Windows?
I had the same problem. I'm using Winpython32 and trying to import win32com. Worked everywhere (I tried) except in PyCharm. sys.path and os.environ['PYTHONPATH'] had some extra entries inside Pycharm, but nothing is missing compared to when run elsewhere.
The solution was to start Pycharm within the Winpython console and not using the shortcut.
sys.path and os.environ['PYTHONPATH'] did not change. os.environ['PATH'] had several additional entries set, all related to the python installation. At this point I suspect it has to do with "non-standard" installations. Winpython32 tries to be "portable", while other reports of similar problems are when using Enthought or Python(x,y).
Manually adding:
C:\WinPython-32\python-2.7.6\
C:\WinPython-32\python-2.7.6\DLLs
C:\WinPython-32\python-2.7.6\Scripts
to the system path (the global PATH environment variable in Windows) solved the problem without having to run Pycharm within the Winpython command line. Note: C:\WinPython-32\python-2.7.6\Scripts alone did not solve it.
I've had that problem before, and it seemed to get fixed by repairing Enthought.
EDIT: I just checked, one of my f2py projects was still suffering from this exact error. Repairing Enthought did not work. The solution to my problem actually lay in fixing the Windows path variable. You need to make sure c:\Python27\Scripts (or your equivalent) is in the path. Furthermore, and this is VERY important, make sure each entry in the global and user path environment variables has NO trailing slashes. This breaks the GNU make utility on Windows.
Add to your PATH environment variable
C:\Python27
C:\Python27\DLLs
C:\Python27\Scripts
This is a pretty frustrating bug in PyCharm. Even if you set your virtualenv from within PyCharm, the "python console" defaults to the system python. When you installed PyCharm, presumably you used a win32 python on a 64 bit machine.
Go to file>settings>Build, Execution, Deployment>Console>Python Console and change the Python Interpreter from the system version to your virtualenv.
Of course, PyCharm doesn't immediately refresh it. You have to close your project and reopen it.
To verify this was successful, open the Python Console (Tools>Python Console) and check the very first line of the output: it should point to the python.exe of your virtual environment, not the system python.
I have the same problem (with another package) and I don't want to modify windows path.
I could solve it very roughly in the pycharm console, by creating a python script which updates sys.path and os.environ['PATH'] in file -settings-console-pathon console - starting script.It works only when I use "tools- python shell"
Still it doesn't work in the run options. I even tried to create a virtual environment, add the path to the activate.bat but in pycharm it doesn't work (from command prompt it does) (as suggested in virtualenv that can find relocated libraires (like mysqlclient lib for MySQLdb) )
I explicilty set the path in the python interpreter option but it doesn't work.(as suggested in https://stackoverflow.com/a/24206781/1136458)
I couldn't find the link but I saved this pic - hopefully works for you
I apologize for my explanation being long and probably not the best of clarity, but this is the best I could do to describe my experience.
I was having the same problem after first installation and this is how I solved it:
I had noticed there are some settings, as indicated in other answers, that tell pyCharm which interpreters and environment managers to use and I was sure the problem was with setting these options, but I was not sure how, so I started searching.
Setting interpreter
I had more or less followed the standard tutorial, assuming in some point I would have used newly created virtualenv inside my project folder. I was initially getting the DLL error, but as I made sure the interpreter settings were pointing at a valid executable.
Here it is explained how to do this.
Setting environment
at this point the error disappeared, but I had progressed towards a new error.
The problem at this point I was not being able to import any module as they were not found.
This is because I was working inside the newly created virtualenv, basically a pristine installation, with no modules installed. I am sure there are more evolved solutions (install modules in the virtualenv), but I was just looking for making the code to work, so I set the environment to use my anaconda usual development environment.
For me, with conda on windows (after activating the development environment with conda activate) then (see ..../anaconda-python-where-are-the-virtual-environments-stored for other systems).
where python gives the path to the interpreter
conda info --envs gives me the existing environment that I want to use.
This fixed everything for me.