Python question from newb

I didn't know where to post this so i came here.

So i started reading the book called Learning Python. From the start there are many examples that you kinda do along as you read. And from the start i ran into a problem. First cmd wouldn't start python. So i googled and did the Path thing and it worked. Now python starts, but if i add anything to it, it doesn't. For example Python HelloWorld.py. It says it can't locate it. If i just drag and drop the file itself in the cmd window right after typing python it works. And that kindof fixes the problem. But there is a new problem when i try to import things.

% python
>>> import myfile
>>> print(myfile.title)
The Meaning of Life

This is the example given. Inside myfile.py is just one like Title = 'The Meaning of Life'

So when i try to type in import myfile it sais "No module name 'mylife'. So the question is, where does python look for the module, and how do i change the directory he is looking at.

windows 8. My windows and python are on SSD, while i wanna keep all the practice things on my HDD.

Other urls found in this thread:

stackoverflow.com/questions/448271/what-is-init-py-for
blog.bahraniapps.com/gifcam/
cmder.net/
twitter.com/NSFWRedditVideo

bump

get the win7 or install ubuntu on second drive

Windows is a little bit gay it probably wants a more qualified location from you when you are rubbing things in cmd. Either absolute path or a proper relative path(./dongs.py).

For importing modules that aren't installed via pip, again it probably needs a more qualified location. For this, you can set your working directory and then import, and it should work? Use the os module and chdir method.

os.chdir('C://path/to/module/dir')
import dongs

There is also __init__.py which should help I think.

idk I haven't used python on Windows in a while. It's fine, just slightly less intuitive than a Linux environment

Not exactly the fix im looking for. Any other suggestions?

On windows you should probably use dreampie

so this is not the way its done?

>dreampie
Well Im starting from the very bottom. And trying to follow the book. Just to get the general idea of wtf im doing at least.

Development is much easier and intuitive on a Unix based OS like Ubuntu or OSX. Windows is geared towards "casual" users like grandparents, not for developers.

I'm sure there will come the time that i will get ubuntu, but for now, for the basics i think windows should work. I mean it should cause so much trouble from the very beginning

try this:

import os
os.chdir('C:\Users\Aleksandr\Programming')
import MyLife


also look at dis

stackoverflow.com/questions/448271/what-is-init-py-for

also use dreampie, cmd sucks

Nah, you would need to:
import os
os.chdir('full_directory_path_of_your_module/your_file.py')
import your_file


By the way, I've been coding in Python for 3 years and have never done this. I'm also 100% sure that it's bad practice.

If you write code and want to turn it into a shared module (e.g. code that needs to be accessed by multiple projects in multiple folders), then you should install it where pip installs all its modules. (depends on your OS)

Considering you're a beginner, I think you're getting off on the wrong foot. You shouldn't be worrying about this kind of stuff yet IMO. Stick more to the basics before learning about __init__.py and module creation type stuff

Also don't include the "your_file.py" in the os.chdir(), I just included that to illustrate how the filename will be the same as your import name. But again, you really don't have to worry about this stuff right now IMO.

...

Fuck why don't they explain this shit in the book. I've been stuck on this for the whole day lol

>for the basics i think windows should work.
it does work, but windows is quirky and not linux, which is your most likely production environment for any project that other people will interact with

See if this works:
import os
here = os.path.join(os.path.expanduser('~'), 'Programming')
os.chdir(here)

type it out yourself and use tab auto complete. also see

Thats the thing, its not supposed to be something i worry about. In the book it's like chapter 2. All it says is "Create MyFile.py, put title = text" then go to CMD and type
python
import myfile

Sup Forums is being a pile of shit and doesn't want to let me post.

Anyways, make a new folder on your desktop, put both python files in it and run the command prompt from that folder.

Before opening python, navigate to the directory which holds the python file ('C:\Users\Aleksandr\Programming') using good old cd within CMD.

THEN, open python and import your file. This will save you from having to os.chdir() within python itself.

>If i just drag and drop the file itself in the cmd window right after typing python it works.
you can do that?

lol i was shocked too

I'm pretty sure even XP and Linux distros from 10 years ago let you do this. Also don't use cmd if you can, there are a billion alternatives.

So python is way the fuck out there C:\Users\Aleksandr\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.6

While all the shit i need to start is here

C:\Users\Aleksandr\Programming

I assume the problem is that they are not in the same place. But when i start the cmd in the folder where all the simple programs are I can start the helloworld.py with python, but i can't import the myfile.py

>39 KB
I like this. How'd you go about making it?

yeah the problem is the working directory for the python process. normally, in a python file, you can import other python files easily since the script you are running is usually in the same directory or in a parent directory

for example if i have dongs.py in ~/Documents/project, and fresh_dongs.py in ~/Documents/project, then import fresh_dongs should work when I run dongs.py

if fresh_dongs.py is in a subdir, like ~/Documents/project/pocket then my import statement in ~/Documents/project/dongs.py would look like import pocket.fresh_dongs

i figured that cmd is the shitties options, but im going along the book, to learn the basics.

I may have been confusing before, but you don't include the ".py" with the import. So it'll be

import myfile


If your file is named "myfile.py"

blog.bahraniapps.com/gifcam/

Reduced framerate, quantization, GIF transparency and some other things. You can probably do better with a screen recorder and a bunch of ffmpeg shit, but it's good enough and I'm lazy.

>I assume the problem is that they are not in the same place. But when i start the cmd in the folder where all the simple programs are I can start the helloworld.py with python, but i can't import the myfile.py

Yep, that is the problem.

Thats the thing, the files themselves are in the same folder, just python is somewhere else. I assume that import looks for my module in the python directory. Yes/no?

but i should be able to just type out the place where the file i want to import is and it should work?

My god, Windows is such a pain in the ass.

...

oh boi

Try capitalizing it maybe? import MyLife

And disable "hide extensions for known file types" in Explorer's settings, otherwise you end up with that .py.py.py shit and not being able to tell what anything is.

AFAIK Python does not use support full PATH for the imports. It does support relative importing (using ../../), but again this is all irrelevant for your case because in the real word, the file would be installed as a global module and imported with simply its name and no PATH needed.

I don't know how to fix your problem because I don't know how the python PATH was setup for Windows. On something like Ubuntu you literally don't have to think about this. It just works

oh my god... Why the fuck would capital letters matter. What is this shit? A whole day i've been googling for this?

Anyway, thanks to everyone who tried to help. Sup Forums for the rescue as always.

Capitalizing won't do anything useful in his case. The filename is all lowercase, so the import will be as well.

Thanks Obama

Oh shit, nevermind, I didn't see that his filename *was* capitalized.

Yeah, it's case sensitive. It's standard to keep filenames all lowercase as well.

The file in the book is lower case. But if you look at my file 2 letter are capitalized. Which has fixed the whole problem.

From now on i will... This was a painfull lesson lol

>What is this shit? A whole day i've been googling for this?
Aye. Welcome

Heh. Anyways, have fun OP. And if you want to be able to at least copy and paste things in the command prompt, try this.

cmder.net/

And use the simple questions thread next time or you'll have people shitting on you.

Python is, but Windows and NTFS isn't. I'm pretty sure you can type pYThOn sCrIptNaMe.pY and have it still work, but not do the same from within Python itself.

Will do, thanks again

escape `\` like so "C:\\User\\Aleksandr\\...

Or use r prefixed raw strings.
Or just use forward slashes, they should work everywhere except in cmd exe

>Command line programming in Windows
Found the problem. If you're gonna program in Windows, do it in a language where you can use all GUIshit. Or, install cygwin to build an environment similar to Linux and go from there. I'd say install Linux, but you don't seem like you'd really do too well with that.

There is a russian version of Sup Forums that you might be interested in -> 2ch.hk

download atom with the script plugin

Since i live in the US i can't post on 2ch. I wish i could since they have a whole board dedicated to programming.

yeah, they have a board even for radio tech! also, you can use vpn, can't you? i mean just for browsing imageboards speed restrictions shouldn't be a problem. try protonvpn. it's free as in free beer.

>protonvpn
Good advice, thank you! I'll look into it.

no problem desu