How/where does Python look for modules? - python

I am completely new to Python and wanted to use py2neo and tornado module.
In order to do that I ran setup.py for both modules and placed them into folders
C:\Python32\modules\py2neo
and
C:\Python32\modules\tornado
In the main program I guess these lines tell the interpreter where to look for files:
import sys
sys.path.append(r'C:\Python32\modules')
# Import Neo4j modules
from py2neo import neo4j, cypher
Reading the book I also added environmental variable (in Windows 7)
PYTHONPATH = C:\Python32\modules;C:\Python32\modules\tornado;C:\Python32\modules\py2neo
Edit
Now I figured out that Python Shell has to be restarted in order to load modified PYTHONPATH variable
In case the variable value is PYTHONPATH = C:\Python32\modules
and the program contains the line
from py2neo import neo4j, cypher
then the following lines are useless:
import sys
sys.path.append(r'C:\Python32\modules')
When I run the program however I get the following error:
Traceback (most recent call last):
File "C:\...\Python Projects\HelloPython\HelloPython\Hellopy2neo.py", line 15, in <module>
from py2neo import neo4j, cypher
File "C:\Python32\modules\py2neo\neo4j.py", line 38, in <module>
import rest, batch, cypher
ImportError: No module named rest
In the file neo4j.py there are the following lines:
try:
import json
except ImportError:
import simplejson as json
try:
from urllib.parse import quote
except ImportError:
from urllib import quote
try:
from . import rest, batch, cypher
except ImportError:
import rest, batch, cypher #line38
and rest.py file is located in folder C:\Python32\modules\py2neo so I don't know why I get the error
ImportError: No module named rest
Edit2:
Trying to import the py2neo directoy in Python Shell and list modules I get:
>>> import py2neo
>>> [name for name in dir(py2neo) if name[0] != '_']
['rest']
I guess there are some unneccesary imports as well and would be very thankful if anyone explained, which imports should be added and excluded (in PYTHONPATH and scripts) in order the program to run without errors.

I suspect the problem is that the import syntax for relative imports has changed in transition from Python 2 to Python 3:
The only acceptable syntax for relative imports is from .[module]
import name. All import forms not starting with . are interpreted as
absolute imports.
The modules you installed use the syntax that would work in Python 2. You could either install them for Python 2, or look for a version of py2neo that supports Python 3, or try to port it manually (the import line should look like from . import rest, but you'll probably face other problems later) or with 2to3 tool.
Update: I tried installing py2neo with pip. It failed for Python3 and finished successfully for Python 2. The version is 1.2.14.

Related

Fixed python pip error spiraled into rabbit hole of solving errors for scripts within scripts within scripts

So I have a hobby I like to do where I program automated systems that upload things to social media with no human input (and make a little side cash). But anyways, I've been trying to crack YouTube recently. YouTube has their own little way to upload videos automatically, but they watch over it INTENSELY. So, I found a work around using youtube_uploader_selenium, a cool little github project that allows you to upload as many videos as you want to YouTube through selenium firefox.
I know if I can get it to work, I can have a fully automated YouTube channel, however one single, important line of code doesn't work properly.
So the module is accessed via an import command in my main script
from youtube_uploader_selenium import YouTubeUploader
I was able to get that working, however when I did I got an error within the module from this line of code saying that selenium_firefox wasn't a valid pip/module.
from selenium_firefox.firefox import Firefox, By, Keys
So, I did some digging and found a PipInstaller script I could use.
import subprocess
import sys
py_exec = sys.executable
# ensure pip is installed & update
subprocess.call([str(py_exec), "-m", "ensurepip", "--user"])
# install dependencies using pip
subprocess.call([str(py_exec),"-m", "pip", "install", "selenium_firefox"])
That had some odd error involving Rust and the Cryptography pip so I decided to take a different approach. I tracked down the selenium_firefox github then downloaded the module from there. In the script I added a line which made it look for the module selenium_firefox in a certain folder where it was downloaded.
from typing import DefaultDict, Optional
from selenium_firefox.firefox import Firefox, By, Keys # problem code
from collections import defaultdict
import json
import time
from .Constant import *
from pathlib import Path
import logging
import platform
selenium_firefox.path.append(
"C:\\Program Files\\Blender Foundation\\Blender2.92\\2.92\\python\\PythonModules\\modules\\")
That <kinda???> worked. This is the error I currently have,
Traceback (most recent call last):
File "C:\Users\CLASSIFIED\Downloads\ASAS\zxxxs.blend\CLASSIFIEDSCRIPTNAME", line 5, in <module>
File "C:\Program Files\Blender Foundation\Blender 2.92\2.92\python\PythonModules\modules\youtube_uploader_selenium\__init__.py", line 2, in <module>
from selenium_firefox.firefox import Firefox, By, Keys
File "C:\Program Files\Blender Foundation\Blender 2.92\2.92\python\PythonModules\modules\selenium_firefox\__init__.py", line 1, in <module>
from .firefox import Firefox
ModuleNotFoundError: No module named 'selenium_firefox.firefox'
Error: Python script failed, check the message in the system console
I don't know where to go from here. The module within the module within the module has errored. This is what the script for the script that's currently producing this error.
from .firefox import Firefox
from .models import *
from .firefox_addons import *
from selenium_browser import *

pybedtools: ImportError: cannot import name scripts

I am trying to import Pybedtools in Spyder.
from pybedtools import BedTool
This is the error I am getting:
Traceback (most recent call last):
File "<ipython-input-13-7a8ea5d1dea8>", line 1, in <module>
from pybedtools import BedTool
File "/Users/michaelsmith/anaconda2/lib/python2.7/site-packages/pybedtools/__init__.py", line 9, in <module>
from . import scripts
ImportError: cannot import name scripts
I just downloaded Anaconda and there doesn't seem to be a reason as to why this happens. What is the typical protocol for resolving bugs like this?
UPDATE:
So within my pybedtools folder there is a scripts folder (which is presumably the module we're trying to import). I changed both the command within __init__.py to:
from . import scripts2
and changed the name of the folder to scripts2 as well. However, I still get the error as such:
ImportError: cannot import name scripts2
So I must be doing something wrong here, which module should I be renaming exactly? Sorry if this is a silly question, I am quite new to python.
This is caused because Anaconda has a module named scripts and therefore your import is "shadowed" by that module. You can double check that when you call import scripts in a new notebook it works even if you have never defined such a module. A very good explanation of import traps can be found here:
http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html
A workaround would be to rename the script module of pybedtools to something else and also change all the imports to the new name.

Import pexpect in child module - 'no module named pexpect'

I'm seeing weird behavior that I don't understand, so I'm turning to the experts here on SO for help. I have looked at similar questions, without finding anything that looked helpful.
I'm writing a program that imports two custom modules.
If I run the main program using the cli "./ld_config_automation.py" I get the following error:
laptop$ ./ld_config_automation.py
Traceback (most recent call last):
File "./ld_config_automation.py", line 34, in <module>
from modules.networkcomponent import NetworkComponent
File "/path/to/pprograms/ssh_telnet_automation/modules/networkcomponent.py", line 7, in <module>
import pexpect
ImportError: No module named pexpect
However, if I run the main program using the cli "python ld_config_automation.py" everything works fine and the program runs.
My main program uses the following import statements.
import sys
import getopt
from modules.networkcomponent import NetworkComponent
from modules.recordclass import Record
The module I'm having trouble with "NetworkComponent" uses the following import statements:
import re
import pexpect
Also, if I remove import pexpect from the NetworkComponent module it works as expected without python in front (up to when it has to use pexpect. of course). So it doesn't seem to be a problem with importing modules, as re works ok, just not pexpect.
I'm developing this on mac os X but will implement on CentOS.

python - Import breaks, because library cannot import its own modules

I'm trying to use mongokit in my Pyramid project, but when I import it, I get the following error.
File "foo.py", line 5, in <module>
import mongokit
File "/home/.../mongokit/__init__.py", line 33, in <module>
from cursor import Cursor
ImportError: No module named 'cursor'
There is a cursor.py file with a Cursor class in it in the same folder as __init__.py.
According to the documentation, when you import a module, it is supposed to put its own directory at the beginning of the search path. However, when I print out sys.path from both foo.py and __init__.py, it is the same in both places.
I can't find anyone else having this problem.
Edit: I am using Python 3.3
mongokit is not compatible with python 3.
From their version notes:
v0.9.0
now MongoKit requires PyMongo >= 2.5
The same error may occur if you use python 3.x and try to import some module for python 2.x.

import error in python on ubuntu

I have created my first python module on ubuntu. When I'm trying to import the module in python using :
import brian
it is giving error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named brian
I have brian in /home/noamaan and python is in /usr/bin.
If you launch python from the directory that contains brian module, everything will work as it is now.
To import custom module from anywhere you want you should read attentively something on the import mechanism in python to learn where the imported modules are searched for, etc.
But to make your code work right now, I can recommend you the following:
Either extend your PYTHONPATH variable before running python, to include the directory of your module
Or append it right in the code by using sys module in this way.
import sys
sys.path.append("path/to/module/dir")
import brian
Also, see info on site module
by default Python import modules from Python path var.
You can view these paths so:
import sys
print sys.path

Categories

Resources