Pycharm doesn't copy root folder to PYTHONPATH - python

I'm using Pycharm community edition 2017.3 in Windows 10.
I'm running Python 2.7 with venv.
Till today all was good, but all of a sudden, PyCharm stopped adding the root folder to PYTHON path, and things stopped working (unless I add it manually to sys.path). I haven't change anything in the PyCharm configuration (at least not explicitly...). I tried closing and reopening PyCharm, reboot, clear the cache, cleaning Python compiled files, setting the root folder as 'sources root', but none seems to work.
The same code exactly is deployed on another computer and it's working.
This is the bad line:
from TM2_VAL_LAB import test_base, robot_handler
And this is the error:
Traceback (most recent call last):
File "C:/Users/baruchl/Documents/_GIT/TM2_VAL_LAB/TM2_VAL_LAB/test_vr_translation_ff.py", line 17, in <module>
from TM2_VAL_LAB import test_base, robot_handler
ImportError: No module named TM2_VAL_LAB
When I run the following during the python execution:
print os.getenv('PYTHONPATH')
I get:
None
When I run the same code in the remote computer (where everything running smoothly), I get:
C:\GIT\TM2_VAL_LAB
which is the root folder of the python script I'm running.
This is the run configuration I'm using:
This is how my project looks like:
Any ideas?

After few back and forth with PyCharm rep, the solution is to close PyCharm, rename/delete the .idea folder and re-open PyCharm.
That's it.
After this, when when running the same file as before,
print os.getenv('PYTHONPATH')
results in:
C:\Users\baruchl\Documents\_GIT\TM2_VAL_LAB
and
print sys.path
results in:
['C:\\Users\\baruchl\\Documents\\_GIT\\TM2_VAL_LAB\\TM2_VAL_LAB', 'C:\\Users\\baruchl\\Documents\\_GIT\\TM2_VAL_LAB', ...]

Related

Custom modules not read by Python, generating ImportError

I am trying to run a python pipeline through Mac OS Terminal. I am using Python 2.7.15 because newer versions of Python gave me error messages with this file. I keep getting the following error message:
$ python ../../../scripts/gemmaPipeline.py ../../../data/all.fixGL.Q10.ap.bi.MAF5.vcf.gz outfile Results
Traceback (most recent call last): File
"../../../scripts/gemmaPipeline.py", line 5, in
import utils, vcf, genomes100, gemma ImportError: No module named genomes100
The modules listed in the import command are present under the "scripts" folder in a subfolder "modules". For each of the four modules, I have a .py and .pyc file in this subfolder. I cannot get Python to find this path.
I have tried running the same script within a virtualenv but to no avail. I even tried moving the "modules" subfolder to the Python "site-packages" folder in hopes that it would work but failed.
Finally, I tried including an init.py file (blank) in the "script" folder, but that didn't work either.
I am an utter novice and am pretty much learning as I go.Please help.

naoqi 2.5.5.5 and PyCharm CE 2017.1

I have an annoying problem with PyCharm CE and naoqi. Following the installation instructions in Aldebaran Guide, I have configured my environment to work with this one.
I have installed the version of python (python 2.7.13) suggested by Aldebaran, I have written the .bash_profile using:
# Setting PATH for Python 2.7
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
#NaoQI 2.5.5
export PYTHONPATH=${PYTHONPATH}:/Users/francesco/Documents/PyLibs/pynaoqi-python2.7-2.5.5.5-mac64/lib/python2.7/site-packages
export DYLD_LIBRARY_PATH=${DYLD_LIBRARY_PATH}:/Users/francesco/Documents/PyLibs/pynaoqi-python2.7-2.5.5.5-mac64/lib
after that, I launch my code in python from Terminal (making sure that the right version of python is called when I type
$ python my_code.py
and all is working (I tried also with virtualenv and all works fine if I launch all from terminal).
So far so good but then comes the poison arrow. I use PyCharm CE like IDE, I have created a new project that use the same environment that works fine in terminal and the magic happens..
The first issue is:
/Users/francesco/Documents/PyEnv/pynaoqi255_python2713/bin/python /Users/francesco/PycharmProjects/PepperRobot/PepperRobot.py
Traceback (most recent call last):
File "/Users/francesco/PycharmProjects/PepperRobot/PepperRobot.py", line 5, in
import qi
ImportError: No module named qi
(Ok seriuosly this is the same environment that in terminal works)
I try to bypass the problem, adding the content of naoqi sdk in the virtualenv's site-package. Ok, the problem is bypassed.. But there is another problem:
/Users/francesco/Documents/PyEnv/pynaoqi255_python2713/bin/python /Users/francesco/PycharmProjects/PepperRobot/PepperRobot.py
Traceback (most recent call last):
File "/Users/francesco/PycharmProjects/PepperRobot/PepperRobot.py", line 5, in
import qi
File "/Users/francesco/Documents/PyEnv/pynaoqi255_python2713/lib/python2.7/site-packages/qi/init.py", line 88, in
from _qi import Application as _Application
ImportError: dlopen(/Users/francesco/Documents/PyEnv/pynaoqi255_python2713/lib/python2.7/site-packages/_qi.so, 2): Library not loaded: #loader_path/libqipython.dylib
Referenced from: /Users/francesco/Documents/PyEnv/pynaoqi255_python2713/lib/python2.7/site-packages/_qi.so
Reason: image not found
Process finished with exit code 1
I did not understand why...
I have also include in
Preferences -> console -> Python Console -> Environment Variables
the same variables included in .bash_profile but to no avail.
How can I solve this tedious problem ?
I have solved my problem editing run/debug configuration and putting here the environment variable DYLD_LIBRARY_PATH. All works fine.

Import statement works on PyCharm but not from terminal

PyCharm 2016.2.3, Mac OS X 10.11.1, Python 3.5 (Homebrew);
I have this folder structure
project
/somepackage
/subpackage
__init__.py
bar.py
__init__.py
foo.py
foo.py:
import somepackage.subpackage.bar
print("foo")
bar.py:
print("bar")
So my expected output is
bar
foo
This works fine when run from PyCharm. However, when I run it from my terminal I get an ImportError:
$ pwd
$ /home/project (not the actual path; just omitting some personal stuff)
$ python3.5 somepackage/foo.py
File "foo.py", line 1, in <module>
import somepackage.subpackage.bar
ImportError: No module named 'somepackage'
I have found this question, which is about the same problem. However, none of the suggested solutions work for me, as I am indeed using the same Python interpreter as PyCharm does and I am currently in the folder that contains the /somepackage folder.
Does anyone have any other suggestions about how to solve this issue?
You are running foo.py like a script, but you are really using it like a module. So the proper solution is to run it as a module:
python3 -m somepackage.foo
For the record, another alternative is to edit your path like:
export PYTHONPATH=.
(Or you could put the absolute directory in there, and of course you should append any other directories that are already in your PYTHONPATH.) This is closer to what PyCharm does, but is less philosophically correct.
Setting PYTHONPATH is what makes it work, as noted above. I use the following VSCODE .env content so that it works for any project:
PYTHONPATH=${PROJ_DIR}:${PYTHONPATH}
This is essentially what PyCharm does when you check "Add Content Roots to PYTHONPATH" in your run/debug configuration. It's a helpful setting, but it spoils you because your code fails outside PyCharm.
Or, if you run in terminal, first export:
export PYTHONPATH=...
Took me days to work all this out.
i solved my problem by two steps on Linux:
first step
go to the root directory of your project and set:
export PYTHONPATH=$PATHONPATH:`pwd`
second step
run python3 -m somepackage.foo
remember Without '.py' suffix
I just had the same problem using scapy.layers.http Module,
this problem occurred on my Kali (linux-Debian) but run fine on Win-10 (after few modifications.)
packet was installed (scapy-http) correctly and the program was running in PyCharm but not as script (from terminal)
I tried solving it with reinstalling in main root, and messing with the sys.path but None have worked.
Troubleshoot & Solution
I found that it looks for the http module in:
/usr/local/lib/python3.7/dist-packages/scapy/layers/init.py
and got the ImportError:
from scapy.layers import http --->
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'http' from 'scapy.layers' (/usr/local/lib/python3.7/dist-packages/scapy/layers/__init__.py)
So then I checked where scapy-http is really installed, which is module called http.py, so I just copied it to:
/usr/local/lib/python3.7/dist-packages/scapy/layers/
found the http.py file in :/usr/local/lib/python3.7/dist-packages/scapy_http/
And that did it :)!
I know its a bit glitchie but that worked!

$PYTHONPATH not working on OSX

I have been at this for a day and have found no solution. My python project is very levels deep and some of the modules have to be run independently (no -m flag, no relative imports). For this reason we decided to add the root folder to the PYTHONPATH. I am running OSX 10.11.3. Everything had been running smoothly until yesterday. I could run independent modules from the terminal with no issues in both Python 2.7 and 3.5. Yesterday, without modifying my PYTHONPATH or any other environment setting, running any of these independent modules from the terminal now gives me import errors in both Python 2 and 3.
Here is my working tree:
/Users/sintrafico/Documents/code/central_maestra
- reports_server.py
- api_server.py
- sintrafico
- sql
- SQLConnection
- api
- incident
- csv (not package)
- independent_module
- tests
I was running my tests yesterday with coverage with no problem, but now I can't.
Python 2 python csv/independent_module.py gives error:
Traceback (most recent call last):
File "csv/independent_module.py", line 13, in <module>
from sintrafico.sql import SQLConnection
ImportError: No module named sintrafico.sql
Python 3 python3 csv/independent_module.py gives error:
Traceback (most recent call last):
File "csv/create_bemobile_csv.py", line 13, in <module>
from sintrafico.sql import SQLConnection
ImportError: No module named 'sintrafico'
From within the incident folder, yesterday this command was working fine: coverage run -m unittest discover. Now all the tests fail because ImportError: No module named 'reports_server'.
Since that started happening yesterday, I have been messing with my PYTHONPATH. I left it blank and it didn't work (as expected), I have added the path now several ways:
export PYTHONPATH=“${PYTHONPATH}:/Users/sintrafico/Documents/code/central_maestra"
export PYTHONPATH=“/Users/sintrafico/Documents/code/central_maestra:${PYTHONPATH}"
export PYTHONPATH=“${PYTHONPATH}:/Users/sintrafico/Documents/code/central_maestra/"
export PYTHONPATH=“/Users/sintrafico/Documents/code/central_maestra:${PYTHONPATH}"
And also setting it without appending the PYTHONPATH since it was empty to begin with. I restarted my computer several times with no success. Also note, I have a .bash_profile and .bashrc with the following contents:
# Setting PATH for Python 3.5
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.5/bin:${PATH}"
export PATH
PYTHONPATH=“/Users/sintrafico/Documents/code/central_maestra:${PYTHONPATH}“
export PYTHONPATH
And lastly, what bugs me the most is that if I run the tests inside PyCharm they all run and pass, I get no import errors at all.
Have you tried setting the path without the quotes and braces? You are setting the python path as a string representation of the path instead of a list of paths

importError using a virtualenv from cmd on Windows

trying to run a flask dev server on localhost:5000 using a virtualenv on Windows 7
In my command line in the project directory, i activated the virtualenv with the command "env\scripts\activate". It seemed to work, as my next line was preceded with an (env) tag. When I attempted to run the app file (bank_app), however, I got an import error.
Here's the console log
C:\Users\TJ\Documents\Python Projects\TestingPython> env\scripts\activate
(env) C:\Users\TJ\Documents\Python Projects\TestingPython> bank_app
Traceback (most recent call last):
File "C:\Users\TJ\Documents\Python Projects\TestingPython\bank_app.py", line 1, in <module>
from flask import Flask
ImportError: No module named flask
and here's a gist of the bank_app file (just in case it's relevant)
I'm used to running the code directly from PyCharm, which handles the virtualenv for me. it works fine running directly from PyCharm
This is probably related to how Windows maps extensions to executables.
You started the script with bank_app, which is really not the name of your script (your script has a .py extension I assume?). Windows must be doing a search in your directory, and then starting the script with the interpreter that is registered for the .py extension of the script, which is the globally installed interpreter, not the interpreter that is currently in the PATH.
I'm pretty sure if you run the command as python bank_app.py everything will work just fine.

Categories

Resources