Module importing in python 2.7 - python

this is my first post and I've been working with python for about a month now.
Anyway I've been trying to import downloaded modules into python 2.7, such as django and mechanize, and I keep getting the same error telling me there is no such module. I downloaded both with pip and from I understand should be in the correct directories. A while ago I decided to simply copy and paste the django folder into the same folder I was saving my programs into, and that took care of the issue. That seems a bit messy though, having to save all of my modules into various folders. So I looked around stackoverflow (and elseware) and pasted that same folder into a PYTHONPATH directory and that didn't solve the problem either. I apologize for the long question but I've been at it for a while now and tried a few different fixes. Any feedback would be greatly appreciated, thank you very much.
On a side note, default modules work fine, such as: math and os. Side note, I'm running kubuntu 14.04.
Thanks again.

The standard Python solution to installing packages is to install pip (package manager) and virtualenv, then make a virtualenv for each project and install your packages in the virtualenv. The documentation for those projects is quite good and should be able to get you started.

Related

Visual Studio Code recognizing Python Packages: Selecting Different Interpreters Doesn't Solve The Problem

I've been trying to move from JupyterLab to Visual Studio Code for Python coding but I have been facing a large difficulty with respect to access to some packages. Probably the issue is that VS Code is very specific with the interpreter path and doesn't look for the existence of a package in a different folder.
I have tried to select running it with all available interpreters. Didn't work. I tried to install it in a specific version. Didn't help either. Trying directly pip install PACKAGENAME didn't work either.
For instance,import seaborn from bs4 import BeautifulSoup. For both of these packages VS Code could not find it. I tried a lot of ways but the only solution for me (very inefficient one) was to copy a bs4 and seaborn into the following folder:'/opt/anaconda3/lib/python3.8/site-packages/'
I am sure more instances might arise with other 'missing' packages for VS Code, and it takes some unnecessary effort. Can someone help me with this? Do you have any idea why this is the case?
Thanks!
VSCode is nothing more then a text editor + a terminal shell. Running a Python script from VSCode is exactly as running it from any other terminal.
Whether you're on a Windows or Unix machine, you can solve your "missing packages" problem once and for all by learning how to work on virtual environments
https://realpython.com/python-virtual-environments-a-primer/

Cannot import any modules to my python project

So few days ago I realized I couldn't import a module called "nbt" that I recently installed, I didn't really bother too much about it because I didn't really have a big need of it.
So I kept going on with my main project, and managed to launch it yesterday and started working for the next update of the project. I needed a few more modules, and started doing some research, found a few and installed them. Now when I try to import the module, it says "Unable to import 'module-name'. And I got confused, installed a few more modules, and same on those too.
I checked to make sure they were being installed in the correct directory, and they are! All the modules that do work are there, the same as the ones I recently installed.
I have done some research, but I don't seem to figure out the issue! I tried adding paths, 'PYTHON_HOME'. And connected that to my 'python path' in my C drive. Still, it does not work!
Also tried re-installing python, no luck. I tried to re-install the 'Python' extension on VSC, and as you might know, did not work.
obs:
I have tried to make new python files in different directories, and that didn't work either.

How to install python developer package on Windows?

I ran into the same problem as here. So I tried to install python developer package but I didn't find the way to do this.
Cython's helloworld instruction doesn't contain any info about it, which seems strange and stupid to me to not include basic requirements into instruction, named "Basic Tutorial" (Sorry if I don't get something about it.)
Anyway... I'm using Windows 10, PyCharm, virtual environment with python 3. When I went to package install window, search result contained 5 packages:
So, which one?
I tried to install one, but it gives me an error:
I met this error in the past with some other package already. PyCharm's suggestion is to run that command from terminal. But from my experience that never helped. Usually solution was either to change name (because some names are right for Python 2.x, other for Python 3.x), or to download the package (egg/wheel) manually and install it offline.
This question was asked before already, but that one is for linux, which is not the case for me.
Can someone save some time for me and future generations, please? ;)
How do I install it and make Cython's helloworld example work?

How to install a custom, local Python Package with Anaconda?

I have Anaconda2 running smoothly on Eclipse's PyDev environment.
I have received a custom package from a colleague in the form of a folder with a "library" sub-directory that contains many ".pyc" files (which I presume are the function files) and a "init.py" file. But no matter what I do, I cannot seem to install the folder as a package.
I have tried everything posted here in the Anaconda Prompt (which I'm assuming was the correct way of implementing the instructions)
http://conda.pydata.org/docs/using/pkgs.html#install-non-conda-packages
but nothing worked.
I am very new to really working with Anaconda, Python, Eclipse, and PyDev (I have only written simple scripts with the default IDLE IDE in the past).
All I really want to be able to do is to use the package of functions given to me - even if they are not properly "installed", although that would be ideal. If anyone out there can help me with this I would be very grateful!
Pyc are precompiled files, you dont need them.
You should simply import package folder with
import folder-name

import openpyxl in django

I am quite new to Python and Django. I have a problem with integrating a python package (openpyxl) to my django app. I'd like to use the methods of these files into my views.py file.
My problem is first that I don't know where's the best place to put the openpyxl folder containing all the files in my file hierarchy.
My hierarchy looks like this:
http://imgur.com/t4iOX98
Is it well placed? Should I put it outside the international folder? inside the carte_interactive folder?
And my biggest problem is inside the __init__.py of openpyxl. I get errors lines like this one:
from openpyxl.xml import LXML
Where there is no resolved reference to LXML, but is actually defined in the xml file of openpyxl.
Is it my bad file placement that caused this? or is it Django?, or is it openpyxl's fault? Do anyone have an idea?
You can see openpyxl's source files here, where I downloaded them:
https://bitbucket.org/openpyxl/openpyxl/src
If you need any more details, please ask!
Thanks in advance!
I applaud your enthusiasm for wanting to learn Django while being new to Python. That said, the way you have things set up right now will make your life unnecessarily difficult to manage.
I would first recommend reading up on best practices for setting up a Django project. Just doing a quick google search for "Django project layout best practices" will give you a lot of resources, but they'll all essentially tell you to do what's in the SO answer above.
The second very basic thing is using pip to install and use other python packages. This is especially important for a django project, where you often have a lot of dependencies outside of Django. Pip is a program to install additionaly python packages. They get installed in your PYTHONPATH, which is just a list of filepaths on disk where python will look for additional packages. If you're on a *NIX system, this is usually in something like /usr/lib/python2.7/. Once you have something in your python path, you can from any piece of code, use other libraries you've installed via the python import system. Essentially, all this more or less does is look through each location in your PYTHONPATHs for the library you're trying to import.
Finally, in regards specifically to lxml, you will want to install it via apt or some other package installer. (e.g. on ubuntu, apt install python-lxml
In order to keep track of all your external python-dependencies, stuff them in a file named "requirements.txt" in the top level directory. This is a pretty standard thing to do for Django projects, so don't worry about shipping code with ALL dependencies inside the project.
Thanks to all of you! I'm using Jetbrains Pycharm and when I wrote import openpyxl, it gave me the choice to install the package. I suppose it does it with pip, which would certainly have worked the same. And I put the package in requirements.txt, so that other users would only have to install this requirement!
It works now! And thanks for the link on the best practices. I'll read that!

Categories

Resources