Updating PYTHONPATH variable pointing to a dropbox directory containing a space - python

I'm trying to import a module for python that I have written that is contained in a Dropbox folder whose path contains a space. Following the comments here, I don't want to do a sys.path.append(path_to_repository) every time I use python, I'd rather just update my bash profile to point to the correct Dropbox folder once.
I've tried adapting the code from the previous page by appending the following lines to my ~/.bash_profile:
PYTHONPATH ="/Users/myusername/Dropbox (projectname)/REPOSITORY_NAME"
export ${PYTHONPATH}
When I close the terminal window and reopen, I get the following error message:
-bash: PYTHONPATH: command not found
-bash: export: `/Users/myusername/Dropbox': not a valid identifier
-bash: export: `(projectname)/REPOSITORY_NAME': not a valid identifier
and (not suprisingly) when I then try to import from the repository in python, I get a module not found:
>>> from REPOSITORY_NAME import myfile
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named REPOSITORY_NAME
Does anyone have any solutions?
Some questions/possibilities I'm thinking of -
1) Does it have anything to do with my Anaconda configuration? (Anaconda runs in a virtual env)
2) Does it has anything to do with the folder with python code being installed in Dropbox?
3) Could it be that spaces in pythonpath are not interpreted correctly?
4) Is there any problem with this being the same directory that syncs to github and bitbucket?
Thanks in advance for your help.
*Edit:
The solution seems to be to 1) erase the extra space in the first line and 2) repalce the ${PYTHONPATH} with PYTHONPATH in the second line, i.e. to adjust ~/.bash_profile to have the following line:
PYTHONPATH="/Users/myusername/Dropbox (projectname)/REPOSITORY_NAME"
export PYTHONPATH

I had a similar problem a while ago. The error message is indicating that the problem lies in the space in the directory path - the bash_profile is being truncated by the space and it is splitting the path into 2. It may be a problem with the way bash handles spaces, but I am not 100% sure.
Here is one solution that worked for me:
export PYTHONPATH="/Users/myusername/Dropbox (projectname)/REPOSITORY_NAME"
It is similar to what you have, but export and PYTHONPATH are in the same line. I don't think this would interfere with Dropbox, Github, Bitbucket, Anaconda (or any other virtualenv like Enthought) etc., as long as you as have a _init__.py' file in each directory where you have your .py files.
Hope this helps

Yeah, you put a space before the = sign when exporting PYTHONPATH:
PYTHONPATH ="/Users/myusername/Dropbox (projectname)/REPOSITORY_NAME"
Should be:
PYTHONPATH="/Users/myusername/Dropbox (projectname)/REPOSITORY_NAME"

Related

add all-subdirectories wildcard to $PATH environment variable (bash)

Here's my directory structure (creddump/ is a program I downloaded directly from github that is not supported by any package manager):
/usr/local/bin/
---> creddump/
---> pwdump.py (imports from framework)
---> framework/
---> types.py
---> lsadump.py
---> another_program/
---> x.py
I'd like to be able to run pwdump.py and x.py simply by typing pwdump.py and x.py, not their full paths. So, I added their paths to my $PATH environment variable by adding it to the beginning of this line in the ~/.bashrc file like this:
export PATH=/usr/local/bin/creddump/:/usr/local/bin/another_program/:~/.local/bin:/snap/bin:/usr/sandbox/:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/share/games:/usr/local/sbin:/usr/sbin:/sbin:$PATH
That's great, but I don't want to manually add another line to $PATH every time I want to download another program. I did some digging and found out that bash supports wildcard expansion. I uncommented this line in ~/.bashrc: shopt -s globstar and I added /usr/local/bin/**/: to the beginning of the export PATH line above. After, I did source ~/.bashrc and/or I restarted the terminal during my various attempts. But, it didn't work;
bash: pwdump.py: command not found
I also tried adding it to the middle and end, but nothing worked.
I know this should be able to work because I've gotten it to work before I upgraded my Parrot Security OS system from 4.10 to 4.11. I've even tried adding the export PATH line to ~/.profile as well.
Any ideas on how to fix this? Thank you so much!
For whom tommy2111111111's incredibly simple solution (for which I can't find an explanation) doesn't work, here's an alternative in accordance with documentation:
d=(/usr/local/bin/**/)
PATH="${d[*]/%\//:}"$PATH
so when I experiment on my own system, this is how I see the path variable once I added the globstar pattern: ...:./path1:./**/ - which is not reading all the paths (perhaps pattern matching is not part of PATH, dont know)
If you are not creating new subdirectories all the time and just want "all subdirectories that currently exist to PATH variable", then a for loop to upgrade your PATH variable would suffice (like is mentioned here: https://unix.stackexchange.com/questions/17715/how-can-i-set-all-subdirectories-of-a-directory-into-path/17856#17856)
but if you want to achieve objective "add all future subdirectories to my PATH"; then in that same answer link, there is a mention of http://www.gnu.org/software/stow/ which seems like its solving your usecase
OMG!!! I fixed it! Instead of adding /usr/local/bin/**/, I just did /usr/local/bin/** without the ending /. I also had to edit the ~/.bashrc file with sudo nano ~/.bashrc. Thank you to everyone else who responded; your solutions work perfectly for me, but I was just looking for a much simpler solution.
You can try something like this. Just name the file file.txt
add_path() {
declare -a new_path
local IFS=:
mapfile -t new_path < <(find /usr/local/bin/ -type f -name '*.py')
NEW_PATH=${new_path[*]%/*}
export PATH="$PATH:$NEW_PATH"
}
add_path
Just source that file.
source ./file.txt
Then try to execute your python scripts without the absolute paths.

Windows 10: PIL.Image.open(abc).load(xyz) triggers OSError: Unable to locate Ghostscript on paths

I'm using Python on Windows 10 with PyCharm. My script contains this line:
img = PIL.Image.open(io.BytesIO(ps.encode('utf-8')))
It triggers this error:
Traceback (most recent call last):
File "C:/Users/x/Desktop/ytg2/main.py", line 504, in <module>
generate_terrain(driver)
File "C:/Users/x/Desktop/ytg2/main.py", line 129, in generate_terrain
img = open_eps(ps, dpi=95.5)
File "C:/Users/x/Desktop/ytg2/main.py", line 32, in open_eps
img.load(scale=math.ceil(scale))
File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\EpsImagePlugin.py", line 332, in load
self.im = Ghostscript(self.tile, self.size, self.fp, scale)
File "C:\Users\x\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\EpsImagePlugin.py", line 134, in Ghostscript
raise OSError("Unable to locate Ghostscript on paths")
OSError: Unable to locate Ghostscript on paths
Process finished with exit code 1
So what I understand is that the function load of the object returned by PIL.Image.open uses the package Ghostscript that can't be found with the interpreter.
So here is, in the order, what I've tried to do:
In PyCharm's packages manager, I've installed the following packages: python3-ghostscript and ghostscript.
In Windows 10 Environments Variables, I have added this variable: (name="Ghostscript" ; value="C:\Program Files\gs\gs9.52\bin\gswin64.exe"). Previously, I've of course manually installed Ghostscript (https://www.ghostscript.com/download/gsdnld.html). I 've tried this value too: %ProgramFiles%\gs%\gs9.52%\bin%\gswin64.exe.
However the problem is still here. What could I do?
The PIL.Image.open(io.BytesIO(ps.encode('utf-8'))) uses shutils.which('gswin64c') to find gswin64c (I knew that by clicking on a file link that the Python Interpreter shown in the PyCharm's console, in the error logs - this link is: one of the two last lines beginning with the word File in the error logs I've shown in the OP, if I remember well).
shutils.which('gswin64c') was returning None (indeed, I made myself a print of it) ; so I prompted os.environ["PATH"] and indeed, it was not contained in the printed output. Then to be sure, I typed echo %path% in the Windows 10 CLI, and I made the same constatation.
My conclusion was: I thought I was correctly adding the path of gswin64c in the way I mentionned in the OP of this SOflw Question (via the admin panel) but in fact, I was wrong.
(Maybe this step is optional.) So: first, since it doesn't work, I have deleted the path of gswin64c that I have added via the admin panel (cf.: the OP). This deletion was done via the admin panel too.
Then, to correctly add the path of gswin64c, I've typed, in the Windows CLI: setx path "%path%;c:\Program Files\..........\" (this path must contain gswin64c). Then I've restarted Windows 10 (if I remember well, it was required).
Then I re-printed the result of shutils.which('gswin64c') and gswin64c is found now. Also os.environ["PATH"] and echo %path% correctly output the path of gswin64c.
I hope this answer could help someone. In fact it was not very difficult: one just has to know how to correctly add a path on Windows 10.... Lol.
I was "inspired" by: https://www.windows-commandline.com/set-path-command-line/ ;-) .

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!

ndiff - PYTHONPATH environment variable

I have a problem with Ndiff, I can't execute it. (Nmap works perfect)
So I try to use ndiff and get this error message:
Could not import the ndiff module: 'No module named ndiff'.
I checked in these directories:
/usr/local/bin
/usr/local/bin
/usr/local/bin/ndiff
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC
/Library/Python/2.7/site-packages
If you installed Ndiff in another directory, you may have to add the
modules directory to the PYTHONPATH environment variable.
So I search for the path of ndiff and export it to PYTHONPATH. But it doesn't work out, what I am doing wrong?
which ndiff
/usr/local/bin/ndiff
I re-install ndiff and nmap with homebrew and know I get this message, after I try to execute this code
$ ndiff firstScan.xml secondScann.xml > diffScan
Traceback (most recent call last):
File "/usr/local/bin/ndiff", line 84, in <module>
sys.excepthook = ndiff.excepthook
AttributeError: 'module' object has no attribute 'excepthook'
The command you're trying to run /usr/local/bin/ndiff, is a Python script in disguise (that is, it doesn't have the .py extension).
Somewhere at the top of that script, it tries to import the ndiff module itself:
import ndiff
which, seen the path /usr/local/, is probably installed in /usr/local/lib/python2.7/site-packages/(*). You need to add this path to your PYTHONPATH:
export PYTHONPATH=${PYTHONPATH}:/usr/local/lib/python2.7/site-packages
Now, the script can pick up the module, and happily continue.
(*) it may get installed somewhere else, in which case you'll have to locate it manually. You could try something like find /usr/local -name ndiff.py to see where it is installed.
The second error, after your re-install, could be caused by a loose ndiff.py somewhere in your current working directory. In that case, /usr/local/bin/ndiff will try to import that file, and think it's the actual ndiff module. Since it's not, it will fail when it tries to access some of the modules attributes like ndiff.excepthook, which that loose ndiff.py doesn't have. Remove the loose file and you should be good to go.
It's probably not necessary to change the PYTHONPTH environment variable, what happens is that the ndiff script is pointing either to a wrong path or to a wrong/non-existent file.
Just edit the ndiff script (passed as a binary) located likely in /usr/bin/ndiff:
# sudo vi /usr/bin/ndiff
and locate the line: INSTALL_LIB = '...
It should read the full path to your python site-packages directory, for example:
INSTALL_LIB = '/usr/lib/python2.7/site-packages'
Seek for a file named ndiff.py in there:
# sudo find /usr/lib/python2.7/site-packages -name ndiff.py
If there is none present, you must create it and paste all the content present here in it: https://raw.githubusercontent.com/nmap/nmap/master/ndiff/ndiff.py
It should all work then.

Setting Environmental Variables for Python on a Mac - seems not to work?

This Drives me nuts...
I use the latest EPD Distribution on the latest Mac and I want to study the book "Python for software design". Therein, Chap. 4, is one should load to play around with.
But to do that I seemingly need to set a PATH variable to the relevant directory. As I am not very familiar I looked around and finally added this to my .bash-profile:
"PYTHONPATH="$HOME/Dropbox/Programming/swampy-2.0"
The check with the Terminal:
Andreass-Mac-mini:~ ak$ $PYTHONPATH
-bash: /Users/ak/Dropbox/Programming/swampy-2.0: is a directory
Up to this point I would say it should work. And then comes this:
>>> from TurtleWorld import *
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
from TurtleWorld import *
ImportError: No module named TurtleWorld
It is the right file name, the correct directory and it seems to me that the path is not right?
So what do I have to do/change that it works on my Mac?
Edit: and by the way - (on a different) I also created a directory .MacOSX and put the file environment.plist therein with the same path variable. Nothing changed :-(
Thanks
Andreas
Use the export command in your .bash-profile to make environment variables appear in other contexts:
export PYTHONPATH=$HOME/Dropbox/Programming/swampy-2.0
Did you name your script Turtleworld.py? If so, that is the issue. The module can't be imported if your script name is the same as the module name. Hope this helps! :D

Categories

Resources