PYTHONPATH showing empty in ubuntu 13.04 - python-2.7

when i do echo $PYTHONPATH it returns nothing..empty line.
so what does that mean. Im using python and it's working fine ..so whats the use of pythonpath and what should be the value of this in ubuntu 13.04
/usr/bin/
or
/usr/lib/
..or something else
and in windows we have python27/source directory where i could put external sources/drivers , where(or equivalent) it is in ubuntu.
when I do user#user$ dpkg -L python2.7 it shows
/.
/usr
/usr/lib
/usr/lib/python2.7
/usr/lib/python2.7/lib-dynload
/usr/lib/python2.7/lib2to3
/usr/lib/python2.7/lib2to3/fixer_util.py
....
/usr/lib/python2.7/lib2to3/Grammar.txt
/usr/share
/usr/share/doc
/usr/share/doc/python2.7
/usr/share/doc/python2.7/NEWS.gz
/usr/share/doc/python2.7/README.Debian
/usr/share/doc/python2.7/ACKS.gz
/usr/share/doc/python2.7/README.gz
/usr/share/doc/python2.7/copyright
/usr/share/lintian
/usr/share/lintian/overrides
/usr/share/lintian/overrides/python2.7
/usr/share/applications
/usr/share/applications/python2.7.desktop
/usr/share/menu
/usr/share/menu/python2.7
/usr/share/man
/usr/share/man/man1
/usr/share/man/man1/2to3-2.7.1.gz
/usr/share/man/man1/pdb2.7.1.gz
/usr/share/man/man1/pygettext2.7.1.gz
/usr/share/man/man1/pydoc2.7.1.gz
/usr/share/pixmaps
/usr/share/pixmaps/python2.7.xpm
/usr/bin
/usr/bin/2to3-2.7
/usr/bin/pygettext2.7
/usr/bin/pydoc2.7
/usr/share/doc/python2.7/changelog.gz
/usr/share/doc/python2.7/changelog.Debian.gz
/usr/bin/pdb2.7
I've downloaded chrome driver from this site and put in given directory/usr/bin..but it's not working .where should i put this?
https://code.google.com/p/selenium/wiki/ChromeDriver

The variable PYTHONPATH that you echo in the terminal is added to the other paths of python. So if you don't have any particular path set in your .profile or .bashrc file (or locally), the variable will be empty.
To see the path that python uses do in a python shell
import sys
print(sys.path)
Or as #mgilson suggestes, you can run from terminal
python -c 'import sys; print(sys.path)'
A note: If you decide to install by hand a package using python setup.py install --user you don't need to add $HOME/.local/lib/pythonX.X/site-packages to PYTHONPATH, as it is already in sys.path

If you want Python to have some extra set of paths in it's sys.path in every Python session apart from the default ones (site-packages etc) you add it to the $PYTHONPATH environment (local or system) variable.
Most probably you don't need it right now, leave it as it is.
Plus you'll know when you really need it populate it.
If you use site.addsitedir("path") in almost every Python path then you can add that "path" to $PYTHONPATH.
Check virtualenv.

Related

Switch between anaconda and miniconda (miniforge)

I have installed conda using miniforge. Since my mac has a m1 chip, i had to install conda using Miniforge3-MacOSX-arm64.sh, inorder to get tensorflow working. unfortunately this version (minforge/minconda arm64) doesn't have python2 for some reason. As I require python2 for another project (doesnot require tensorflow) I have decided to install anaconda3.
But now I am unaware how to switch between the two conda versions (anaconda3 and miniconda/miniforge3).
For example when I enter activate conda in the terminal, it activates the base environment of the miniforge version.
How do I activate base environment of the anaconda version. So that I can create python2 environment there (anaconda3).
According to this post, one solution is to change the content of your .zshrc file, save your changes, close and reopen your terminal. I tested on a MacBook Pro M1 where Miniforge3 and Anaconda3 are currently installed and it works. In the following, just replace --PATH-- with the path of the requested environment management system. For example, I replace --PATH-- with opt/anaconda3 for Anaconda3 and miniforge3 for .. Miniforge3.
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Users/username/--PATH--/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/Users/username/--PATH--/etc/profile.d/conda.sh" ]; then
. "/Users/username/--PATH--/etc/profile.d/conda.sh"
else
export PATH="/Users/username/--PATH--/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
A quick fix for switching between environments is to pick out the path you get from the output of conda env list. Here is what I get from both miniforge and miniconda:
(base) user#machine script % conda env list
# conda environments:
#
base * /Users/user/miniforge3
nmgp /Users/user/miniforge3/envs/nmgp
scphere /Users/user/miniforge3/envs/scphere
/opt/miniconda3
/opt/miniconda3/envs/gpcounts
/opt/miniconda3/envs/gpy
/opt/miniconda3/envs/test
/opt/miniconda3/envs/nmgp
/opt/miniconda3/envs/scphere
/opt/miniconda3/envs/ssdgp
To activate the miniforge environments you can use the name directly:
conda activate nmgp
To activate a miniconda environment you can use the absolute path:
conda activate /opt/miniconda3/envs/nmgp

Adding gradle to PATH variable using python 2.7 on Centos 7

I'm new to python and I've been working on a script that will automatically set up development environments. So i have a laundry list of things I need to add to the script and one of them is the latest gradle-5.4.1. I'm running into an issue when I try to add gradle into $PATH on Centos7. After I run this function and run a gradle -v and check the $PATH, it's never concatenated into PATH variable. We need to be able to run gradle from anywhere and I cant seem to figure out how to do this.
def install_gradle():
print("Initiating gradle 5.4.1 install.....")
os.system("sudo wget https://services.gradle.org/distributions/gradle-5.4.1-bin.zip -P /tmp")
os.system("sudo unzip -d /opt/gradle /tmp/gradle-5.4.1-bin.zip")
os.environ("export PATH=$PATH:/opt/gradle/gradle-5.4.1/bin")
I've tried that last line with both os.system and os.environ, neither worked so not sure how to get this to work using python.
Thanks in advance for any input.
So this is what worked for me, editing the bashrc file seemed to resolve the my question:
def install_gradle():
print("Initiating gradle 5.4.1 install.....")
print("Downloading gradle file to tmp directory")
os.system("sudo wget https://services.gradle.org/distributions/gradle-5.4.1-bin.zip -P /tmp")
print("Unzipping gradle installation to /opt/gradle")
os.system("sudo unzip -d /opt/gradle /tmp/gradle-5.4.1-bin.zip")
print("Changing directories to /etc/bashrc")
bashrc_file = open("/etc/bashrc", "a+")
print("Appending PATH environment variables to bashrc file")
bashrc_file.write("export PATH=/opt/gradle/gradle-5.4.1/bin:$PATH")
bashrc_file.close()

Why conda doesn't find correct PYTHONPATHS of the local virtual enviroment?

Edit: The aim of this work is to build a conda environment to replace bash exports. Therefore bash exports has been disabled.
I have created a local conda virtual environment named my_env using pre installed conda specification file called my_env.txt (same as requirment.txt )
conda create --prefix <path/to/my_env_directory> --file <path/to/my_env.txt>
i.e. conda create --prefix /hjs/jen/projects/my_env --file /hjs/jen/projects/project1/my_env.txt
And I have placed my paths.pth file in /hjs/jen/projects/my_env/lib/python2.7/site-packages/paths.pth
However, it doesn't find the correct local PYTHONPATHS as shown below.
When I run python -c "import sys; print '\n'.join(sys.path)" to check PYTHONPATHS, I get
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/hjs/jen/project/my_env/lib/python2.7/lib-dynload
/net/home/j/jen/.local/lib/python2.7/site-packages
/usr/local/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages/PILcompat
/usr/lib/python2.7/dist-packages/gtk-2.0
So basically conda should not find any PYTHONPATH in /net/home/j/jen/.local/lib/python2.7/site-packages as it is NOT in local env but user specific (it's in home and jen is the user name) but it does!
To test this I've created a new test_env.pth file at hjs/jen/projects/my_env/lib/python2.7/site-packages/test_env.pth and sure enough it did not find this new PYTHONPATH
However, when I recreate the environment and delete both *.pth files and replace them in /net/home/j/jen/.local/lib/python2.7/site-packages/test_paths.pth, conda does find all the PYTHONPATHS as before as well as /hjs/jen/test_test
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/hjs/jen/project/my_env/lib/python2.7/lib-dynload
/net/home/j/jen/.local/lib/python2.7/site-packages
/net/home/j/jen/.local/lib/python2.7/site-packages
/hjs/jen/test_test
/usr/local/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages/PILcompat
/usr/lib/python2.7/dist-packages/gtk-2.0
So basically I don't want conda to find any PYTHONPATH environment variables in /net/home/j/jen/.local/lib/python2.7/site-packages/ but ONLY in hjs/jen/projects/my_env/lib/python2.7/site-packages/. How do I achieve this?
Further info: which python shows only one installation of python inside the env
I am using python 2.7

Django Install Wierdness - Mac OSX 10.8.5 and Macport Python 2.7.6 - Django/Python/Unix beginner

I've found a lot of posts on Macports/Django install issues but none seem to quite address my situation.
Installed Django using Macports from the command line using
sudo port install py27-django
This seemed to work fine. I opened up IDLE and was able to import django. The following bit of code
import django
from django.core import management
print django.VERSION
returns
(1, 5, 1, 'final', 0)
Which I take to mean I have the final version of Django 1.5.1 installed. So, all seems to be well.
However, now I switch over to my Django tutorial to get learning and I am asked to type this into the command line to confirm that Django is installed:
python -c "import django; print(django.get_version())"
and get the following error:
> File "<string>", line 1, in <module> ImportError: No module named
> django
Some other bits of data. Here's my $PATH:
/Library/Frameworks/Python.framework/Versions/2.7/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin
Finally, the command
django-admin.py startproject mysite
returns
-bash: django-admin.py: command not found
But the command
django-admin-2.7.py startproject mysite
works (creates a new directory called mysite in the working directory). I read somehwere that Macports renames the django-admin file.
I've only been working with Unix for a few weeks so this is a bit confusing. It seems like I have Django on my system, but perhaps the python I am trying to access from my command line is a different python from that which Macports uses.
Any help appreciated!
Dennis
You will need to set your PYTHONPATH environment variable from your terminal to reference your django install. IDLE probably did this for you, but in another shell you'll need to set it. You can set it in your terminal permanently by setting it in ~/.bashrc or ~/.bash_profile (or whatever your terminal shell is).
Your PATH starts with /Library/Frameworks/Python.framework/Versions/2.7/bin so when you type python you get the python in that directory which is the Apple supplied one nut you have installed Django for the Macports one so python does not see it.
To fix you need to have /opt/local/bin earlier on the path than the /Library one. even better remove that /Library entry and use port select python ... to choose which python to run the Macports 2.7 or Apple's or others
As for the django-admin-2.7.py issue, Macports allows you to have several versions of python at once so code depending on eth version has the version add into the script names e.g. you could have a django-admin-2.6.py as well if you installed the py26-django package. This also applies to python itself however python also adds into the port select mechanism so you set a default version.

ImportError: No module named twisted.internet

I installed python 2.7.5 which is working fine.
I then install scrapy (which, I think, uses twisted internally). My scrapy spider is also working fine.
I installed twisted:
sudo apt-get install python-twisted
Then, I created a sample program using Echo Server code shown here
Here is the code
from twisted.internet import protocol, reactor
class Echo(protocol.Protocol):
def dataReceived(self, data):
self.transport.write(data)
class EchoFactory(protocol.Factory):
def buildProtocol(self, addr):
return Echo()
reactor.listenTCP(1234, EchoFactory())
reactor.run()
I try to run this code using this command:
$ python twistedTester.py
Traceback (most recent call last):
File "twistedTester.py", line 1, in <module>
from twisted.internet import protocol, reactor
ImportError: No module named twisted.internet
Can anyone help me with how I can debug why my twisted package is not being picked up by Python installation?
If you use pip just try:
pip install twisted
The same works with w3lib and lxml.
On some *nix systems this might give you a permission error. If that happens, try:
sudo -H pip install twisted
I figured out why this error was happening. For some reason, using apt-get to install a python package was not installing it right.
So, I had to download a tar ball and install the package from them.
I downloaded Twisted tar from here.
I did a tar xjf Twisted-13.1.0.tar.bz2 - this created a directory called Twisted-13.1.0
next, cd Twisted-13.1.0
Finally, python setup.py install
This gave me an error. Twisted required another package called zope.interface. So, I downloaded tar ball for zope.interface from here. Then, ran this command tar xzf zope.interface-3.6.1.tar.gz That created a folder called zope.interface-3.6.1. So, cd into zope.interface-3.6.1 and run python setup.py install
Note: Depending on your user's rights, you may want to do these commands in sudo mode. Just add the keyword sudo before every command.
please rename the file twisted.py to something else. whenever you import a function from a file the interpreter will search for the file in the current location and then it searches in the library. so if you have any file in the name of "twisted.py" you should probably rename it.
after renaming it. dont fail to remove the twisted.pyc file before running it again.
It happened to me too. Finally I figure out that there is a file named twisted.py my present working directory. I removed twisted.py and twisted.pyc. Problem resolved.
Looks like Twisted may have removed the twisted.internet module from the current release. Pinning on the version required by scrapy (17.9.0) worked for me:
$ pip install twisted==17.9.0
Checking if it's installed:
$ python -c "import twisted.internet; print(twisted.internet)"
<module 'twisted.internet' from '/Users/username/dev/env/redacted-ewmlD2h2/lib/python3.7/site-packages/twisted/internet/__init__.py'>
I figured out why apt-get install python-twisted was not enough or "installing it right", as you said, user1700184.
I use Debian Wheezy and Python 2.7.
I just had to move the folder named "twisted" from /usr/lib/python2.7/dist-packages/ to /usr/lib/python2.7/
The same has to be done with the package "zope" and any other one that you do install but is not retrieved when you try run your code.
However, why this is even necessary in my case is still a mystery since my sys.path does include both /usr/lib/python2.7/ and /usr/lib/python2.7/dist-packages, so whatever was under dist-packages should have been retrieved by the interpreter.
I think it is worth noting that if you use sudo to launch python you are using your original default system python. This is NOT the python that your PATH points to. For example if you are using Anaconda and you have updated your path such that which python points to path/to/anaconda/bin/python, sudo which python will still point to usr/bin/python.
So obviously sudo python twistedTester.py will not find the twisted module. To get around this you should explicitly pass the path to the anaconda python. Like so:
sudo path/to/anaconda/bin/python twistedTester.py